差分约束POJ3159Candies(spfa+stack)解题报告
来源:互联网 发布:ps软件什么好 编辑:程序博客网 时间:2024/06/04 01:11
Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 26267 Accepted: 7197
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
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly–2006.12.31, Sempr
差分约束题,转化成求最短路径,求点1到点InKidNum的距离。
// 2520K 547MS#include<algorithm>#include<cstring>#include<iostream>#include<stack>#include<cstdio>#define MAXEDGE 150005#define MAXVERTEX 30005#define inf 1000000000using namespace std;int InKidNum,InEdgeNum;int AdjEdge[MAXEDGE];int vis[MAXVERTEX];int dis[MAXVERTEX];//点1到每个点的最短距离struct Edge{ int endV; int w; int PriEdge;}edge[MAXEDGE];void init(){ memset(vis,0,sizeof(vis)); memset(AdjEdge,0,sizeof(AdjEdge));//当遍历到起始点的邻边为0时,结束遍历 fill(dis+1,dis+InKidNum+1,inf); dis[1]=0;//源点到源点距离为0}void addEdge(int startV,int endV,int w,int i){ edge[i].endV=endV; edge[i].w=w; edge[i].PriEdge=AdjEdge[startV]; AdjEdge[startV]=i;}void spfa(){ stack<int> s; s.push(1);//源点入栈 while(!s.empty()){ int frontV=s.top();s.pop();vis[frontV]=0;//栈首出栈 for(int i=AdjEdge[frontV];i!=0;i=edge[i].PriEdge){ if(dis[edge[i].endV]> dis[frontV]+edge[i].w){ dis[edge[i].endV]= dis[frontV]+edge[i].w; if(!vis[edge[i].endV]){//入栈 vis[edge[i].endV]=1; s.push(edge[i].endV); } } } } printf("%d\n",dis[InKidNum]);}int main(){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); while(scanf("%d%d",&InKidNum,&InEdgeNum)!=EOF){ int tmp1,tmp2,tmp3; init(); for(int i=1;i<=InEdgeNum;i++){ scanf("%d%d%d",&tmp1,&tmp2,&tmp3); addEdge(tmp1,tmp2,tmp3,i);//加入第i条边,边从1开始编号 } spfa(); } return 0;}
- 差分约束POJ3159Candies(spfa+stack)解题报告
- poj3159Candies差分约束系统(spfa+栈)
- POJ3159Candies(差分约束系统)
- zoj2770解题报告(差分约束)
- poj 3159 Candies(差分约束 spfa stack实现)
- 【poj 3159】Candies 差分约束+spfa(stack)
- poj3159Candies——差分约束系统
- UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
- 【差分约束】king(国王)解题报告 …
- 差分约束POJ1201NIntervals解题报告
- POJ3169 差分约束系统 解题报告
- Hdu 1529 差分约束 解题报告
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- poj3159 Candies 差分约束+spfa+模拟stack
- poj 3159 Candies(差分约束,dij+heap,spfa+stack)
- POJ 3159 Candies(差分约束 转spfa+stack 求最短路径)
- POJ1364 King(差分约束+Spfa)
- POJ3159差分约束(SPFA)
- 增加widget的水平可调整大小功能
- Linux学习篇-SVN命令详解
- fseek函数
- 1014. 福尔摩斯的约会 (20)
- 晨遇
- 差分约束POJ3159Candies(spfa+stack)解题报告
- Java使用jdbc连接和操作mysql的例子
- windows多线程编程简介(2)
- Android性能专项测试之Heap Snapshot工具
- 寻找字符串中最长回文——Manacher算法及其Java实现
- 字符串压缩
- yum
- 新浪sae开发不能写入文件
- poj 1091-跳蚤(数论+容斥)