POJ-3159 Candies( 差分约束 )

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

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

给N个人分糖果,分糖果时部分学生AB之间存在一条规则,学生B所分得的糖果最多比学生A都C个,现问N号学生最多能比1号学生多分几个
差分约束 一道最基本的差分约束题,即约束为val[a] - val[b] <= k,求N最多比1大多少,可以转化为有向路edge[b][a] = k,然后求1大N的最短路问题
如果用spfa需要用stack,否则会超时,然后,慎用cin


 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<map>
 6 #include<cstdio>
 7 #include<queue>
 8 #include<stack>
 9 
10 using namespace std;
11 
12 const int LENN = 30000 + 5;
13 const int LENM = 150000 + 5;
14 const int INF = 0x3f3f3f3f;
15 
16 struct Edge{
17     int to, next;
18     int val;
19 }edge[LENM];
20 int dis[LENN];
21 bool vis[LENN];
22 int h[LENN];
23 int n, m;
24 
25 void spfa_bfs(){
26     memset( vis, false, sizeof( vis ) );
27     memset( dis, INF, sizeof( dis ) );
28     dis[1] = 0;
29     stack<int> Q;
30     Q.push( 1 );
31     vis[1] = true;
32 
33     while( !Q.empty() ){
34         int x = Q.top(); Q.pop(); vis[x] = false;
35         for( int k = h[x]; k != 0; k = edge[k].next ){
36             int y = edge[k].to;
37             int val = edge[k].val;
38             if( dis[x] + val < dis[y] ){
39                 dis[y] = dis[x] + val;
40                 if( !vis[y] ){
41                     Q.push( y );
42                     vis[y] = true;
43                 }
44             }
45         }
46     }
47 }
48 
49 int main(){
50     //ios::sync_with_stdio( false );
51 
52     cin >> n >> m;
53     int beg, end, val;
54     memset( h, 0, sizeof( h ) );
55     for( int i = 1; i <= m; i++ ){
56         scanf( "%d%d%d", &beg, &end, & val );
57         edge[i].to = end;
58         edge[i].val = val;
59         edge[i].next = h[beg];
60         h[beg] = i;
61     }
62 
63     spfa_bfs();
64     cout << dis[n] << endl;
65 
66     return 0;
67 }

 

posted @ 2016-07-13 14:04  「空白」物语  阅读(220)  评论(0编辑  收藏  举报