zoukankan      html  css  js  c++  java
  • 【差分约束】POJ3159/LG P1993 小K的农场

    终于把差分约束刷完了!!,这些题的套路都是很类似的

    题目描述
    
    小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述:
    
    农场a比农场b至少多种植了c个单位的作物,
    农场a比农场b至多多种植了c个单位的作物,
    农场a与农场b种植的作物数一样多。
    但是,由于小K的记忆有些偏差,所以他想要知道存不存在一种情况,使得农场的种植作物数量与他记忆中的所有信息吻合。
    
    输入输出格式
    
    输入格式:
    第一行包括两个整数 n 和 m,分别表示农场数目和小 K 记忆中的信息数目。
    
    接下来 m 行:
    
    如果每行的第一个数是 1,接下来有 3 个整数 a,b,c,表示农场 a 比农场 b 至少多种植了 c 个单位的作物。
    
    如果每行的第一个数是 2,接下来有 3 个整数 a,b,c,表示农场 a 比农场 b 至多多种植了 c 个单位的作物。如果每行的第一个数是 3,接下来有 2 个整数 a,b,表示农场 a 种植的的数量和 b 一样多。
    
    输出格式:
    如果存在某种情况与小 K 的记忆吻合,输出“Yes”,否则输出“No”。
    
    输入输出样例
    
    输入样例#1: 复制
    3 3
    3 1 2
    1 1 3 1
    2 2 3 2
    输出样例#1: 复制
    Yes
    说明
    
    对于 100% 的数据保证:1 ≤ n,m,a,b,c ≤ 10000
    小K
    Candies
    Time Limit: 1500MS        Memory Limit: 131072K
    Total Submissions: 39184        Accepted: 11031
    Description
    
    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
    
    snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
    
    Input
    
    The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
    
    Output
    
    Output one line with only the largest difference desired. The difference is guaranteed to be finite.
    
    Sample Input
    
    2 2
    1 2 5
    2 1 4
    Sample Output
    
    5
    Hint
    
    32-bit signed integer type is capable of doing all arithmetic.
    Source
    
    POJ Monthly--2006.12.31, Sempr
    Poj3159

    这两道题都比较简单

    小K那个

    第一次因为c++11卡了CE,不要建time数组什么的,是关键字,注意linux编译-std=c++11

    第二次数组开小了QAQ

    第三次忘记建超级原点

    为什么要建呢?

    因为是有向边,从1开始跑会有一些点访问不到,但是你没访问的可能就有负环

    所以建一个超级原点(通常是n+1),到每个点建边权为0的边这样就能解决

    第二题就是判断一个环,然后就完事了

    注意!!spfa最好还是队列!!栈会莫名其妙的WA,dij还是最好用的,稳定nlogn

    第一题的

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #define N 10011
     5 #define clear(a,val) memset(a,val,sizeof(a))
     6 using namespace std;
     7 struct star{int to,nxt,val;}edge[10*N];
     8 int n,m,u,v,w,top,cnt=1,opt;
     9 int head[N],stk[N],dis[N],tim[N];
    10 bool in[N]; 
    11 inline void add(int u,int v,int w){
    12     edge[cnt].nxt=head[u];
    13     edge[cnt].to=v;
    14     edge[cnt].val=w;
    15     head[u]=cnt++;
    16 }
    17 int main()
    18 {
    19     clear(head,-1);
    20     clear(dis,0x3f);
    21     scanf("%d%d",&n,&m);
    22     for(int i=1;i<=m;i++)
    23     {
    24         scanf("%d",&opt);
    25         if(opt==1)
    26         {
    27             scanf("%d%d%d",&u,&v,&w),add(u,v,-w);
    28         }
    29         else if(opt==2)
    30         {
    31             scanf("%d%d%d",&u,&v,&w),add(v,u,w);
    32         }
    33         else if(opt==3)
    34         {
    35             scanf("%d%d",&u,&v);add(u,v,0),add(v,u,0);
    36         }    
    37     }
    38     for(int i=1;i<=n;i++)add(n+1,i,0);
    39     stk[++top]=n+1,in[n+1]=1,dis[n+1]=0;
    40     while(top)
    41     {
    42         int now=stk[top--];in[now]=0;
    43         for(int i=head[now];i!=-1;i=edge[i].nxt)
    44         {
    45             int to=edge[i].to;
    46             if(dis[to]>dis[now]+edge[i].val)
    47             {
    48                 dis[to]=dis[now]+edge[i].val;
    49                 if(!in[to])
    50                 {
    51                     stk[++top]=to,in[to]=1;
    52                     if(++tim[to]>n)
    53                     {
    54                         puts("No");return 0;
    55                     }
    56                 }
    57             }
    58         }
    59     }
    60     puts("Yes");
    61     return 0;
    62 }

    第二题

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #define N 1011
     5 #define M 10011
     6 #define clear(a,val) memset(a,val,sizeof(a))
     7 using namespace std;
     8 struct star{int to,nxt,val;}edge[M*2];
     9 int n,ml,md,u,v,w,top,cnt=1;
    10 int head[N],stk[N],dis[N],time[N];
    11 bool in[N]; 
    12 inline void add(int u,int v,int w){
    13     edge[cnt].nxt=head[u];
    14     edge[cnt].to=v;
    15     edge[cnt].val=w;
    16     head[u]=cnt++;
    17 }
    18 int main()
    19 {
    20     clear(head,-1);
    21     clear(dis,0x3f);
    22     scanf("%d%d%d",&n,&ml,&md);
    23     for(int i=1;i<=ml;i++)
    24         scanf("%d%d%d",&u,&v,&w),add(u,v,w);
    25     for(int i=1;i<=md;i++)
    26         scanf("%d%d%d",&u,&v,&w),add(v,u,-w);//least
    27     for(int i=1;i<n;i++)
    28         add(i+1,i,0);
    29     stk[++top]=1,in[1]=1,dis[1]=0;
    30     while(top)
    31     {
    32         int now=stk[top--];in[now]=0;
    33         for(int i=head[now];i!=-1;i=edge[i].nxt)
    34         {
    35             int to=edge[i].to;
    36             if(dis[to]>dis[now]+edge[i].val)
    37             {
    38                 dis[to]=dis[now]+edge[i].val;
    39                 if(!in[to])
    40                 {
    41                     stk[++top]=to,in[to]=1;
    42                     if(++time[to]>n)
    43                     {
    44                         puts("-1");return 0;
    45                     }
    46                 }
    47             }
    48         }
    49     }
    50     if(dis[n]==dis[0])puts("-2");
    51     else printf("%d
    ",dis[n]);
    52     return 0;
    53 }
  • 相关阅读:
    C#中double转int时需要注意的地方
    OracleHelper类
    POJ2570, ZOJ1967
    ZOJ3088
    POJ3259(spfa判负环)
    POJ3268
    ZOJ1092 POJ2240
    ZOJ1298 POJ1135
    SRM587 div2
    POJ1679判断最小生成树是否唯一
  • 原文地址:https://www.cnblogs.com/Qin-Wei-Kai/p/10167145.html
Copyright © 2011-2022 走看看