首页 > 其他分享 >P4878 [USACO05DEC] Layout G

P4878 [USACO05DEC] Layout G

时间:2024-06-04 14:13:07浏览次数:10  
标签:Layout int P4878 len start 1010 now USACO05DEC dis

原题链接

大概思路

我们已知一组不等式的解可以通过建边然后求最短路/最长路来得出
而这里要求 \(D_n-D_1\) 的最大值,所以我们要求最短路。

补充

为什么要求最短路?
对于任何一组不等式,我们都可以写成 \(a_i-b_i \leq c_i\) 建边含义
假设 \(D_n-D_1\) 有最大值,那么通过这组不等式的组合,一定能得出若干个形似 \(D_n-D_1 \leq k_j\) 的式子,此时便可以得出 \(\max(D_n-D_1)\) 就等于 \(\min(k_j)\)

细节

  1. \(D_i\geq D_{i-1}\ (i>1)\ \to D_{i-1}-D_i\leq 0\)
    2.要判断每条边(即每个约束)是否都满足,所以要建立超级源点,判断是否存在负环,不能以点1来判断是否存在负环,因为图不保证联通

code

#include<bits/stdc++.h>
using namespace std;
const int inf=2e9;

int dis[1010];

struct node
{
    int to,len;
};
vector<node> G[1010];
int n;
bool run(int start)
{
    int vis[1010]={0};
    bool in_q[1010]={0};
    fill(dis,dis+1004,inf);
    queue<int>  q;
    q.push(start);
    in_q[start]=1;
    dis[start]=0;

    while(q.size())
    {
        int now=q.front();
        q.pop();
        in_q[now]=0;
        vis[now]++;
        if(vis[now]>=n)
        {
            return 0;
        }
        for(auto next:G[now])
        {
            int to=next.to,len=next.len;
            if(dis[to]>dis[now]+len)
            {
                dis[to]=dis[now]+len;
                if(!in_q[to])
                {
                    q.push(to);
                }
            }
        }
    }
    return 1;
}
int main()
{
    int l,d;
    cin>>n>>l>>d;

    for(int i=1;i<=n;i++) G[0].push_back({i,0});
    for(int i=2;i<=n;i++) G[i].push_back({i-1,0});
    for(int i=1;i<=l;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        G[a].push_back({b,c});
    }

    for(int i=1;i<=d;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        G[b].push_back({a,-c});
    }



    if(!run(0)) puts("-1");
    else
    {
        run(1);
        if(dis[n]==inf) puts("-2");
        else cout<<dis[n]<<endl;
    }

    //for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
    return 0;
}

标签:Layout,int,P4878,len,start,1010,now,USACO05DEC,dis
From: https://www.cnblogs.com/pure4knowledge/p/18230638

相关文章

  • Java中java.awt.FlowLayout的常量字段值
    流布局用于安排有向流中的组件,这非常类似于段落中的文本行。流的方向取决于容器的componentOrientation属性,它可能是以下两个值中的一个:ComponentOrientation.LEFT_TO_RIGHTComponentOrientation.RIGHT_TO_LEFT流布局一般用来安排面板中的按钮。它使得按钮呈水平放置,直到同一......
  • posterlayout:A new benchmark and approach for content-aware visual-textual presen
    1.introductionPKUPosterLayout包括9974对海报布局和905张图像,DSF设计序列生成算法,一种基于CNN-LSTM的生成对抗网络(DS-GAN),受到图像的条件制约,学习设计序列的分布,从而生成具有内容感知的视觉-文本展示布局。2.RelatedworkLayoutGAN,LayoutGAN++,CGL-GAN。3.ANewBenchmark:......
  • JPanel的GridLayout布局添加网格线
        之前从网上找了许多添加网格线的方法,比如添加JLable,设置JLable文本框线实现添加网格线://初始化显示界面JFramejf=newJFrame("数独游戏");//设置窗口可视化jf.setVisible(true);//设置窗口大小jf.setSize(900,810);//将窗口显示在屏幕中央jf.setLocatio......
  • 布局解析LayoutInflater分析
    布局解析-LayoutInflater分析一般添加布局或控件有两种方式,一种是直接new对应的View,然后通过addView方法添加到父控件中,一种是将布局写在layout的xml文件中,然后调用调用接口添加到父控件中,而这里就涉及到将xml布局转为View控件,一般都是使用LayoutInflater的inflate方法来讲布局xm......
  • css41 CSS Website Layout
    https://www.w3schools.com/css/css_website_layout.asp WebsiteLayoutAwebsiteisoftendividedintoheaders,menus,contentandafooter: Therearetonsofdifferentlayoutdesignstochoosefrom.However,thestructureabove,isoneofthemostcomm......
  • css33 CSS Layout - Horizontal & Vertical Align
    https://www.w3schools.com/css/css_align.asp  CenterAlignElementsTohorizontallycenterablockelement(like<div>),usemargin:auto;Settingthewidthoftheelementwillpreventitfromstretchingouttotheedgesofitscontainer.Theele......
  • css32 CSS Layout - display: inline-block
    https://www.w3schools.com/css/css_inline-block.aspThedisplay:inline-blockValueComparedtodisplay:inline,themajordifferenceisthatdisplay:inline-blockallowstosetawidthandheightontheelement.Also,withdisplay:inline-block,thetop......
  • css31 CSS Layout - float and clear
    https://www.w3schools.com/css/css_float.asp CSSLayout-floatandclear  TheCSSfloatpropertyspecifieshowanelementshouldfloat.TheCSSclearpropertyspecifieswhatelementscanfloatbesidetheclearedelementandonwhichside. Theflo......
  • css29 CSS Layout - The z-index Property
    https://www.w3schools.com/css/css_z-index.asp CSSLayout-Thez-indexProperty  Thez-indexpropertyspecifiesthestackorderofanelement.Thez-indexPropertyWhenelementsarepositioned,theycanoverlapotherelements.Thez-indexproperty......
  • css30 CSS Layout - Overflow
    https://www.w3schools.com/css/css_overflow.aspCSSLayout-Overflow  TheCSSoverflowpropertycontrolswhathappenstocontentthatistoobigtofitintoanarea. <!DOCTYPEhtml><html><head><style>#overflowTest{b......