POJ 3159 Candies差分约束系统 spfa 附:各种最短路比较
来源:互联网 发布:开淘宝网店怎么发货 编辑:程序博客网 时间:2024/06/05 04:55
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
Hint
看到这种谁比谁多的情况就应该想到差分约束系统!
啥是差分约束系统??
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=30005;int dis[maxn],head[maxn];int n,m,sum;bool vis[maxn];struct node{ int v,w,next;}edge[150010];void addedge(int a,int b,int c){ edge[sum].v=b; edge[sum].w=c; edge[sum].next=head[a]; head[a]=sum++;}bool spfa(int s){ int stack[maxn],outque[maxn],top=0; memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); memset(outque,0,sizeof(outque)); stack[top++]=s; vis[s]=1;//vis数组的作用不是判断它是否被更新过 而是是否在栈里! dis[s]=0; while(top){ int tmp=stack[--top]; vis[tmp]=0; outque[tmp]++; if(outque[tmp]>n) return 0; //判断负环,当然这里没有必要写它 int k=head[tmp]; while(k>-1){ if(dis[edge[k].v]>edge[k].w+dis[tmp]){ dis[edge[k].v]=edge[k].w+dis[tmp]; if(vis[edge[k].v]==0){ vis[edge[k].v]=1; stack[top++]=edge[k].v; } } k=edge[k].next; } } return 1;}int main(){ //freopen("cin.txt","r",stdin); while(cin>>n>>m){ sum=0; memset(head,-1,sizeof(head)); int a,b,c; while(m--){ scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); } spfa(1); printf("%d\n",dis[n]); } return 0;}
另:
Dijkstra:适用于权值为非负的图的单源最短路径,用斐波那契堆的复杂度O(E+VlgV)
- POJ 3159 Candies差分约束系统 spfa 附:各种最短路比较
- POJ 3159 Candies(spfa最短路,差分约束)
- poj-3159 最短路SPFA+差分约束系统
- poj 3159 Candies(差分约束原理 最短路)
- POJ 3159 Candies(最短路 差分约束)
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- POJ 3159 Candies【差分约束+SPFA】
- poj 1201 Intervals(最短路+spfa+差分约束系统)
- Candies 3159----差分约束系统+spfa
- 最短路练习11/poj/3159 【差分约束系统】Candies
- POJ 3159 Candies(差分约束系统)
- 【POJ 3159】 Candies 差分约束系统
- POJ 3159 Candies 差分约束系统
- POJ 3159 Candies(差分约束系统)
- 差分约束系统 & poj 3159 Candies
- POJ 3159Candies 差分约束系统
- 《Java设计模式》之装饰模式
- RA layer request failed
- HDU 2176 取(m堆)石子游戏
- 流程图描述的WEP攻击
- Selendroid server 2种启动方式
- POJ 3159 Candies差分约束系统 spfa 附:各种最短路比较
- 十大技巧快速提升Android应用开发性能
- SDWebImage
- java中main方法的详解
- easyUI的右下角弹窗效果
- JS效率问题之 str+ ;array.join && arr[i]
- 在N个乱序数字中查找第k大的数字
- 用Arch Link(Mbed平台)制作的物联网展示
- Product of Array Except Self