poj 3159
来源:互联网 发布:推荐淘宝上的韩国泡菜 编辑:程序博客网 时间:2024/06/03 10:59
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 throughN. 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
Source
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#define maxn 1<<30using namespace std;struct Edge{ int a,b,c; int next;}edge[150005];int dist[30005];int m,n,mm;int fst[30005];bool vis[30005];int cnt[30005];int q[90000000];bool spfa(){ memset(vis,0,n+2); memset(cnt,0,4*(n+2)); for(int i=1;i<=n;i++)dist[i]=maxn; int top=0; q[++top]=1; dist[1]=0; int a,b; vis[1] = 1 ; cnt[1]=1; while(top) { a=q[top--]; vis[a]=0 ; for(int i=fst[a];i!=0;i=edge[i].next) { b=edge[i].b; if(dist[b]>dist[a]+edge[i].c) { dist[b]=dist[a]+edge[i].c; if(!vis[b]) { vis[b]=1; q[++top]=b; cnt[b]++; if(cnt[b]>n) { return false; } } } } } return true;}int main(){ int a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { mm=0; memset(fst,0,4*(n+2)); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); edge[++mm].next=fst[a]; fst[a]=mm; edge[mm].a=a; edge[mm].b=b; edge[mm].c=c; } spfa(); cout<<dist[n]<<endl; } return 0;}
- poj 3159
- poj 3159
- poj 3159
- POJ 3159
- poj 3159
- poj 3159 heap+dijkstra
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 candies总结
- POJ 3159 Candies
- poj-3159,SPFA+堆栈
- POJ 3159 Candies
- poj 3159-Candies
- POJ-3159-Candies
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- 如何下载Latex缺少的宏包
- 1014 Dividing 多重背包
- 1016 Numbers That Count 模拟
- 面对百度原创性质量标准算法的一些策略
- 1017 Packets 贪心
- poj 3159
- 25-闪烁(适合火狐浏览器)
- 1019 Number Sequence 数论
- Curl模仿http浏览器常见命令
- Assignment 3: Data Structures
- 2013 多校第二场 hdu 4618 Palindrome Sub-Array
- 1020 Anniversary Cake DFS
- atheros设置beacon和probe response中的IE信息
- 系统集成项目管理之项目整体管理