3159 Candies SPFA
来源:互联网 发布:r语言如何保存数据 编辑:程序博客网 时间:2024/06/16 06:12
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
Source
const int INF=1<<30-1;
const int E=150001;
const int V=30001;
struct EDGE
{
int link,val;
int next;
}edge[E];
int head[V],dist[V],e;
bool vis[V];
inline void addedge(int a,int b,int c)
{
edge[e].link=b;
edge[e].val=c;
edge[e].next=head[a];
head[a]=e++;
}
int relax(int u,int v,int c)
{
if(dist[v]>dist[u]+c)
{
dist[v]=dist[u]+c;
return 1;
}
return 0;
}
int SPFA(int src,int n)
{
for(int i=1;i<=n;i++)
{
vis[i]=0;
dist[i]=INF;
}
dist[src]=0;
vis[src]=true;
int Q[E],top=1;
Q[0]=src;
while(top)
{
int u,v;
u=Q[--top];
vis[u]=false;//还可以放进来
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].link;
if(relax(u,v,edge[i].val)==1&&!vis[v] )
{
Q[top++]=v;
vis[v]=true;
}
}
}
return dist[n];
}
- 3159 Candies SPFA
- poj 3159 -- Candies ( spfa + 栈 )
- poj 3159 Candies------spfa算法
- POJ 3159 Candies(SPFA+stack)
- poj 3159 Candies (spfa+stack)
- Candies 3159----差分约束系统+spfa
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- POJ-3159-Candies(SPFA+模拟栈)
- POJ 3159Candies 栈模拟SPFA
- SPFA+Stack||Dijkstra+Heap-POJ-3159-Candies
- POJ 3159 Candies(dijkstra+heap&spfa+stack)
- POJ 3159 Candies【差分约束+SPFA】
- poj3159 Candies(SPFA+stack)
- Candies SPFA + 栈
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- poj 3159 Candies (spfa+栈)
- POJ 3159 Candies 差分约束 spfa+栈+邻接表
- poj1118解题报告
- 白领的一天 场景8:面试成功
- 如何一整天有精神,保持微笑
- poj1504解题报告
- 白领的一天 场景9:First day in office为第一天做准备
- 3159 Candies SPFA
- 读书笔记之编程之美 – 2.6 精确表达浮点数
- 白领的一天 场景10:Getting to know your colleague报到上班
- poj1579解题报告
- 白领的一天 场景11:我的办公环境
- SQLHelper.cs
- 白领的一天 场景12:你好!我是新人
- csdn的第一次
- Boost智能指针——shared_ptr