POJ3159 Candies —— 差分约束 spfa

题目链接:http://poj.org/problem?id=3159

 

Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 33576   Accepted: 9422

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

 
 
 
 
题解:
1.有n个点,m对 A B W  表示B-A<=W。
2.在满足上述条件的情况下, 求1和n的最大差值。
3.由B-A<=W 可得出:B<=A+W,这就是最短路径中的松弛,即:dis[v] = min(dis[v], dis[u] + w)。
4.所以对于每一对 A B W, 建一条边:A->B = W,然后跑一下最短路(此题数据量大,要用spfa+栈)。
 
 
 
代码如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
13 #define ms(a,b) memset((a),(b),sizeof((a)))
14 using namespace std;
15 typedef long long LL;
16 const double EPS = 1e-8;
17 const int INF = 2e9;
18 const LL LNF = 9e18;
19 const int mod = 1e9+7;
20 const int maxn = 2e5+10;
21 
22 int n, m;
23 
24 struct edge
25 {
26     int to, w, next;
27 }edge[maxn];
28 int cnt, head[maxn];
29 
30 void addedge(int u, int v, int w)
31 {
32     edge[cnt].to = v;
33     edge[cnt].w = w;
34     edge[cnt].next = head[u];
35     head[u] = cnt++;
36 }
37 
38 void init()
39 {
40     cnt = 0;
41     memset(head, -1, sizeof(head));
42 }
43 
44 int dis[maxn], times[maxn], inq[maxn];
45 void spfa(int st)
46 {
47     memset(inq, 0, sizeof(inq));
48     memset(times, 0, sizeof(times));
49     for(int i = 1; i<=n; i++)
50         dis[i] = INF;
51 
52     stack<int>S;
53     S.push(st);
54     inq[st] = 1;
55     dis[st] = 0;
56     while(!S.empty())
57     {
58         int u = S.top();
59         S.pop(); inq[u] = 0;
60         for(int i = head[u]; i!=-1; i = edge[i].next)
61         {
62             int v = edge[i].to;
63             if(dis[v]>dis[u]+edge[i].w)
64             {
65                 dis[v] = dis[u]+edge[i].w;
66                 if(!inq[v])
67                 {
68                     S.push(v);
69                     inq[v] = 1;
70                 }
71             }
72         }
73     }
74 }
75 
76 int main()
77 {
78     while(scanf("%d%d", &n, &m)!=EOF)
79     {
80         init();
81         for(int i = 1; i<=m; i++)
82         {
83             int u, v, w;
84             scanf("%d%d%d", &u, &v, &w);
85             addedge(u,v,w);
86         }
87 
88         spfa(1);
89         printf("%d\n", dis[n]);
90     }
91 }
View Code

 

posted on 2017-09-30 09:39  h_z_cong  阅读(216)  评论(0编辑  收藏  举报

导航