Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 25821 | Accepted: 7050 |
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
这个题算是spfa算法的一个应用吧, 就是用来找到满足不等式的解
题目大意 :有n个小朋友,有m项限制条件,每一行限制条件有a,b,c三个变量,代表的是b比a最多多c个糖果,求第n个小朋友最多比第一个小朋友多多个糖果
可以想一下,求b比a最多多几个糖果,也就是求b与a之间的最短路径
那么不等式就可以看做求最短路径的问题b-a <= c (dist[v] <= dist[u] + p[i].val 把它变形一下就是dist[v] - dist[u] <= p[i].val) 故我们可以把a,b看做u,v两个节点,把c看做边上的权值,然后套模板就ok了。但要注意一下这是个有向图,只能赋一条边若有言辞不严谨的地方,还请大牛们不吝赐教代码如下:# include<cstdio> # include<cstring> # include<algorithm> # define INF 0x3f3f3f3f # define MAXA 30000 + 100 # define MAXB 150000 + 100 using namespace std; int n, m; int head[MAXA]; int vis[MAXA]; int dist[MAXA]; int cnt; struct node { int from, to, val, next; }p[MAXB]; void init() { cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, int w) { p[cnt].from = u; p[cnt].to = v; p[cnt].val = w; p[cnt].next = head[u]; head[u] = cnt++; } void get() { int a, b, c; while(m--) { scanf("%d%d%d",&a, &b, &c); add(a, b, c);//这里是有向图,只能单边赋值 } } void spfa() { int stack[MAXA]; int top = 0; for(int i = 1; i <= n; i++) { dist[i] = INF; vis[i] = 0; } dist[1] = 0; vis[1] = 1; stack[top++] = 1; while(top) { int u = stack[--top]; vis[u] = 0; for(int i = head[u]; i != -1; i = p[i].next) { int v = p[i].to; if(dist[v] > dist[u] + p[i].val) { dist[v] = dist[u] + p[i].val; if(!vis[v]) { vis[v] = 1; stack[top++] = v; } } } } printf("%d\n",dist[n]); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); get(); spfa(); } return 0; }