1. 程式人生 > >【差分約束】POJ3159 Candies

【差分約束】POJ3159 Candies

Candies
Time Limit: 1500MS        Memory Limit: 131072K
Total Submissions: 39165        Accepted: 11023
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
T

這道題是差分約束,差分約束是什麼?
就是給出很多個條件a-b>=c,可以a->b建一條邊權為c的邊,然後跑最短路,這就是差分約束

這道題能讓我們學到什麼?

1.spfa不要用隊列了,用棧比佇列更快

核心在此

 1 while(top)
 2 {
 3     int now=stk[top--];in[now]=0;
 4     for(int i=head[now];i!=-1;i=edge[i].nxt)
 5     {
 6         int to=edge[i].to;
 7         if(dis[to]>dis[now]+edge[i].val)
 8         {
 9             dis[to]=dis[now]+edge[i].val;
10             if(!in[to])
11             {stk[++top]=to;in[to]=1;}
12         }
13     }
14 }

2.dij比spfa更穩定,是穩定nlogn

程式碼

堆疊版

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 #define N 30011
 5 #define M 150011
 6 using namespace std;
 7 struct star{int to,nxt,val;}edge[M];
 8 int dis[N],head[N],stk[N];
 9 int cnt=1,n,m,a,b,c,top;
10 bool in[N];
11 inline void add(int u,int v,int w)
12 {
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     memset(head,-1,sizeof(head));
21     memset(dis,0x3f,sizeof(dis));
22     scanf("%d%d",&n,&m);
23     for(int i=1;i<=m;i++)
24     {
25         scanf("%d%d%d",&a,&b,&c);
26         add(a,b,c);
27     }
28     stk[++top]=1;
29     dis[1]=0,in[1]=1;
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                 {stk[++top]=to;in[to]=1;}
41             }
42         }
43     }
44     printf("%d\n",dis[n]);
45     return 0;
46 }
47 /*
48 2 2
49 1 2 5
50 2 1 4
51 */

dij版

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 #define N 30011
 5 #define M 150011
 6 using namespace std;
 7 struct star{int to,nxt,val;}edge[M];
 8 struct misaki{
 9     int pos,dis;
10     friend bool operator<(misaki a,misaki b)
11         {return a.dis>b.dis;}
12     misaki(int pos,int dis):pos(pos),dis(dis){}
13 };
14 priority_queue<misaki>q;
15 int dis[N],head[N];
16 int cnt=1,n,m,a,b,c;
17 bool used[N];
18 inline void add(int u,int v,int w)
19 {
20     edge[cnt].nxt=head[u];
21     edge[cnt].to=v;
22     edge[cnt].val=w;
23     head[u]=cnt++;
24 }
25 int main()
26 {
27     memset(head,-1,sizeof(head));
28     memset(dis,0x3f,sizeof(dis));
29     scanf("%d%d",&n,&m);
30     for(int i=1;i<=m;i++)
31     {
32         scanf("%d%d%d",&a,&b,&c);
33         add(a,b,c);
34     }
35     dis[1]=0;
36     q.push(misaki(1,dis[1]));
37     while(!q.empty())
38     {
39         misaki now=q.top();q.pop();
40         if(used[now.pos])continue;
41         else used[now.pos]=1;
42         for(int i=head[now.pos];i!=-1;i=edge[i].nxt)
43         {
44             int to=edge[i].to;
45             if(dis[to]>dis[now.pos]+edge[i].val)
46             {
47                 dis[to]=dis[now.pos]+edge[i].val;
48                 q.push(misaki(to,dis[to]));
49             }
50         }
51     }
52     printf("%d\n",dis[n]);
53     return 0;
54 }
55 /*
56 2 2
57 1 2 5
58 2 1 4
59 */