POJ3159 Candies(差分约束 PS:此题spfa+队列过不了,spfa + stack才能过)
来源:互联网 发布:sql server 2017 sn 编辑:程序博客网 时间:2024/05/16 10:56
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 Bshould 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
本题没什么好说的,简单的差分约束,不过有一个坑点spfa + queue是过不了的,会WA,只能spfa + stack才能过。
AC代码:
#include<iostream>#include<cstring>#include<cstdio> using namespace std;const int maxn = 30010;const int maxe = 150010;const int inf = 0x3f3f3f3f;int head[maxn];int dist[maxn];struct Edge{int to;int w;int next;}edge[maxe];int k;int Min;void init(){k = 0;memset(head,-1,sizeof(head));}void addedge(int u,int v,int w){edge[k].to = v;edge[k].w = w;edge[k].next = head[u];head[u] = k++;}bool SPFA(int s,int n) { int i,k; int u; bool vis[maxn]; int sta[maxn]; int top; top = 0; for(i=0;i<=n;i++) { dist[i] = inf; } memset(vis,false,sizeof(vis)); sta[++top] = s; vis[s] = true; dist[s] = 0; while(top) //非0栈不为空 { u = sta[top--]; //栈顶vis[u] = false;for(int i=head[u];i!=-1;i=edge[i].next){if(dist[edge[i].to] - edge[i].w > dist[u]){dist[edge[i].to] = dist[u] + edge[i].w;if(!vis[edge[i].to]){vis[edge[i].to] = true;sta[++top] = edge[i].to;}}} }} int main(){int m,n;int candy1,candy2,l;int i;//freopen("1.txt","r",stdin);while(scanf("%d%d",&m,&n) != EOF){init();for(i=0;i<n;i++){scanf("%d%d%d",&candy1,&candy2,&l);addedge(candy1,candy2,l);}SPFA(1,m);printf("%d\n",dist[m]);}return 0;}
- POJ3159 Candies(差分约束 PS:此题spfa+队列过不了,spfa + stack才能过)
- poj3159 Candies 差分约束+spfa+模拟stack
- poj3159-Candies差分约束-spfa+栈
- poj3159——Candies(差分约束+SPFA堆栈)
- POJ3159 Candies(差分约束+SPFA的栈实现)
- poj3159 Candies(SPFA+stack)
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- poj 3159 Candies(差分约束 spfa stack实现)
- 【poj 3159】Candies 差分约束+spfa(stack)
- POJ3159差分约束(SPFA)
- Candies 3159----差分约束系统+spfa
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- Candies+差分约束+spfa+poj
- Candies(差分约束_栈+SPFA)
- POJ 3159 Candies【差分约束+SPFA】
- poj Candies(差分约束,spfa栈)
- poj 3259 Candies spfa 差分约束
- 什么会在main函数之前执行
- getResource于getResourceAsStream的使用
- 浅析系统性能调优
- Scala学习第八天 Scala主构造器、私有构造器、构造器重载实战详解
- HDU 5305 Friends DFS+剪枝
- POJ3159 Candies(差分约束 PS:此题spfa+队列过不了,spfa + stack才能过)
- ural 1008 Image Encoding
- Hiberante中的QBC检索方式
- COCOS2D-X项目在XCODE没有IOS Simulator,无法仿真的解决办法
- 使 spine MVC 支持 X-HTTP-Method-Override
- 浅析性能测试培训总结-IP欺骗
- 动态代理
- [POJ 1308]Is It A Tree?[并查集][判断有向树]
- 在线的图片、js、css压缩优化工具介绍