poj3159--Candies(差分约束)
2014-08-22 21:44
381 查看
Candies
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
Sample Output
一个差分约束的模板题,明确的给出了 a b w a比b最多多w,所以 b - a <= w 。 那么由b到a建立有向边,权值为w,跑一次由1到n最短路,得出最多的差值
坑点就是使用queue会超时,只能stack,可能是数据的问题
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 23131 | Accepted: 6224 |
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
一个差分约束的模板题,明确的给出了 a b w a比b最多多w,所以 b - a <= w 。 那么由b到a建立有向边,权值为w,跑一次由1到n最短路,得出最多的差值
坑点就是使用queue会超时,只能stack,可能是数据的问题
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <stack> #define inf 0x3f3f3f3f using namespace std; struct node { int v,w,next; } edge[150010]; int head[30001],dis[30001],vis[30001],cnt,n,m,s; void add(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } void spfa() { for(int i=1;i<=n;i++) { dis[i]=inf; vis[i]=0; } dis[s]=0; vis[s]=1; stack<int >Q; Q.push(s); while(!Q.empty()) { int u=Q.top(); Q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(dis[v]>dis[u]+edge[i].w) { dis[v]=dis[u]+edge[i].w; if(!vis[v]) { vis[v]=1; Q.push(v); } } } } } int main() { int i,j,u,v,w; while(~scanf("%d%d",&n,&m)) { s=1; for(i=1;i<=n;i++) head[i]=-1; cnt=0; for(i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); } spfa(); printf("%d\n",dis ); } return 0; }
相关文章推荐
- POJ3159 Candies(差分约束)
- POJ3159:Candies(差分约束)
- poj3159 Candies(差分约束,最短路)
- poj3159 Candies(差分约束)
- poj3159 Candies(差分约束)
- POJ3159 Candies(差分约束 PS:此题spfa+队列过不了,spfa + stack才能过)
- 差分约束 poj3159 Candies
- POJ3159 Candies(差分约束)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- K - Candies(差分约束)
- poj 3159 Candies 【简单差分约束】 【SPFA + 数组模拟栈】
- poj-3159 Candies(差分约束问题)
- pku 3159 Candies 差分约束
- POJ 3159 Candies(差分约束+dijkstra+heap)
- 2013长沙网络赛J题 Candies(差分约束或数学规律)#by zh
- poj 3159 Candies(差分约束 spfa stack实现)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies(spfa最短路,差分约束)
- K - Candies(Dijkstra差分约束)
- POJ3150 Candies【差分约束】