Candies POJ

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.
 
题意:有 n 个人分糖,有 m 个条件需要满足,每个条件 a b c 表示为:b 的糖数-a 的糖数 <= c,问满足 m 个条件的情况下,要使 n 号的糖数 - 1 号糖数差最大为多少;
 
思路:不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,即if(dis[B]>dis[A]+w(A,B),dis[B]=dis[A]+w(A,B)。是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大,所以这题可以转化为最短路来求。这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用spfa + stack
 
代码:
 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 998244353
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 const int maxn=150010;
21 //n,m代表点数和边数
22 int n,m;
23 //x代表与之相连的点,y代表边的价值,next代表当前边起点的位置
24 struct node
25 {
26     int x,y,next;
27 };
28 //no数组存放边的数据
29 node no[maxn];
30 //head存放每条边的位置,ans表示第几条边
31 int head[maxn],ans;
32 //dis表示起点到其他边的最短长度
33 int dis[30010];
34 //vis表示此点是否已是最短路
35 bool vis[30010];
36 void spfa()
37 {
38     //stackbiqueue更节约时间,
39     stack<int> qu;
40     qu.push(1);
41     //初始化
42     memset(dis,INF,sizeof(dis));
43     memset(vis,0,sizeof(vis));
44     vis[1]=1;
45     dis[1]=0;
46     //为空时退出
47     while(!qu.empty())
48     {
49         int s=qu.top();
50         qu.pop();
51         //由于当钱点数据一边,所以要更新与之相连的所有点直接按的距离
52         vis[s]=0;
53         //从当前点开始直到与之相连的点
54         for(int i=head[s];i!=-1;i=no[i].next)
55         {
56             //en表示与起点相连的点
57             int en = no[i].x;
58             //更新最小操作
59             if(dis[en]>dis[s]+no[i].y)
60             {
61                 dis[en]=dis[s]+no[i].y;
62                 if(!vis[en])
63                 {
64                     vis[en]=1;
65                     qu.push(en);
66                 }
67             }
68         }
69     }
70 }
71 
72 int main()
73 {
74     ans=0;
75     scanf("%d %d",&n,&m);
76     int a,b,c;
77     //初始化
78     memset(head,-1,sizeof(head));
79     for(int i=0;i<m;i++)
80     {
81         scanf("%d %d %d",&a,&b,&c);
82         //存放边
83         no[ans].x=b;
84         no[ans].y=c;
85         no[ans].next=head[a];
86         head[a]=ans++;
87     }
88     spfa();
89     printf("%d
",dis[n]);
90 }
原文地址:https://www.cnblogs.com/mzchuan/p/11521348.html