POJ 3159 Candies
来源:互联网 发布:数据库的英文单词 编辑:程序博客网 时间:2024/05/17 20:27
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 21 2 52 1 4
Sample Output
5
N个小朋友分糖果,B比A最多多C个,求1到n之间最大差距。
差分约束
dia[b]-dis[a]<=c 可以转化成dis[b]<=c+dis[a],求最大差距,所以可以转化成求最短路的 if(dis[b]>dis[a]+c) dis[b]=dis[a]+c
#include <iostream>#include <stdio.h>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#define INF 1<<30using namespace std;int q[33333];int cnt;int head[33333],dis[33333];int vis[33333];struct Node{ int v,w,nt;}f[333333];void add(int u,int v,int w){ f[cnt].v=v; f[cnt].w=w; f[cnt].nt=head[u]; head[u]=cnt++;}void spfa(int st,int n){ int top=0; for(int i=0;i<=n;i++) { if(i==st) { vis[i]=1; q[top++]=i; dis[i]=0; } else { vis[i]=0; dis[i]=INF; } } while(top!=0) { int x=q[--top]; vis[x]=0; for(int i=head[x];i!=-1;i=f[i].nt) { int v=f[i].v; if(dis[v]>dis[x]+f[i].w) { dis[v]=dis[x]+f[i].w; if(vis[v]==0) { vis[v]=1; q[top++]=v; } } } }}int main(){ int n,m; int a,b,c; while(~scanf("%d%d",&n,&m)) { cnt=0; for(int i=0;i<=n;i++) head[i]=-1; for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c);//dis[b]-dis[a]<=c } spfa(1,n); printf("%d\n",dis[n]); } return 0;}
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 candies总结
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159-Candies
- POJ-3159-Candies
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ -- 3159 Candies
- poj 3159 Candies
- Candies ( POJ 3159)
- poj 3159 Candies
- POJ 3159 Candies
- Unity Shader 表面着色器(Surface Shader)
- LeetCode Ugly Number II
- Android中使用VideoView播放视屏
- 八皇后问题
- 文件分隔符、几种创建File对象的方式
- POJ 3159 Candies
- PAT1017 A除以B (20)
- poj 3104
- chan-vese模型
- Android XUtils框架学习及注意事项
- 如何分析一个QT类
- 【codevs10月月赛(第三次)】day 1
- 用sql做apriori商品关联销售分析
- Spring整合CXF发布Web Services