POJ-3159.Candies.(差分约束 + Spfa)

Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 40407   Accepted: 11367

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 AB 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

 
本题大意:n个孩子分糖果,给出m个a,b,c意为b比a分的糖果不能多于c个,即t[ b ] - t[ a ] <= c。很明显是差分约束了,可以转为单源最短路进行求解。
我们可以根据上面的约束方程推出松弛方程为if(dist[b] > dist[a] + edge[i].w) {dist[b] = dist[a] + edge[i].w;}。
 
这题应该是数据问题吧,至今不太明白为什么queue会TLE,而栈可以过...
 
参考代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <stack>
 4 using namespace std;
 5 
 6 const int maxn = 30010, maxe = 150010, INF = 0x3f3f3f3f;
 7 struct node {
 8     int to, w, next;
 9 }edge[maxe];
10 int num, head[maxn], dist[maxn];
11 bool vis[maxn];
12 
13 void addedge(int u, int v, int cost) {
14     edge[num].to = v;
15     edge[num].w = cost;
16     edge[num].next = head[u];
17     head[u] = num ++;
18 }
19 
20 void spfa(int start, int n) {
21     stack <int> Q;
22     for(int v = 1; v <= n; v ++) {
23         if(v == start) {
24             Q.push(v);
25             vis[v] = true;
26             dist[v] = 0;
27         }
28         else {
29             vis[v] = false;
30             dist[v] = INF;
31         }
32     }
33     while(!Q.empty()) {
34         int u = Q.top(); Q.pop();
35         vis[u] = false;
36         for(int i = head[u]; i != -1; i = edge[i].next) {
37             int v = edge[i].to;
38             if(dist[v] > dist[u] + edge[i].w) {
39                 dist[v] = dist[u] + edge[i].w;
40                 if(!vis[v]) {
41                     vis[v] = true;
42                     Q.push(v);
43                 }
44             }
45         }
46     }
47 }
48 
49 int main () {
50     int n, m, a, b, cost;
51     while(~scanf("%d %d", &n, &m)) {
52         num = 0;
53         memset(head, -1, sizeof head);
54         for(int i = 1; i <= m; i ++) {
55             scanf("%d %d %d", &a, &b, &cost);
56             addedge(a, b, cost);
57         }
58         spfa(1, n);
59         printf("%d
", dist[n]);
60     }
61     return 0;
62 }
View Code

 或者用数组模拟栈也可以:

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 const int maxn = 30010, maxe = 150010, INF = 0x3f3f3f3f;
 6 struct node {
 7     int to, w, next;
 8 }edge[maxe];
 9 int num, head[maxn], dist[maxn];
10 int Q[maxn];
11 bool vis[maxn];
12 
13 void addedge(int u, int v, int cost) {
14     edge[num].to = v;
15     edge[num].w = cost;
16     edge[num].next = head[u];
17     head[u] = num ++;
18 }
19 
20 void spfa(int start, int n) {
21     int top = 0;
22     for(int v = 1; v <= n; v ++) {
23         if(v == start) {
24             Q[top ++] = v;
25             vis[v] = true;
26             dist[v] = 0;
27         }
28         else {
29             vis[v] = false;
30             dist[v] = INF;
31         }
32     }
33     while(top != 0) {
34         int u = Q[-- top];
35         vis[u] = false;
36         for(int i = head[u]; i != -1; i = edge[i].next) {
37             int v = edge[i].to;
38             if(dist[v] > dist[u] + edge[i].w) {
39                 dist[v] = dist[u] + edge[i].w;
40                 if(!vis[v]) {
41                     vis[v] = true;
42                     Q[top ++] = v;
43                 }
44             }
45         }
46     }
47 }
48 
49 int main () {
50     int n, m, a, b, cost;
51     while(~scanf("%d %d", &n, &m)) {
52         num = 0;
53         memset(head, -1, sizeof head);
54         for(int i = 1; i <= m; i ++) {
55             scanf("%d %d %d", &a, &b, &cost);
56             addedge(a, b, cost);
57         }
58         spfa(1, n);
59         printf("%d
", dist[n]);
60     }
61     return 0;
62 }
View Code
原文地址:https://www.cnblogs.com/bianjunting/p/10708430.html