【POJ 3159】Candies(差分约束+SPFA)
来源:互联网 发布:可可网络验证后台 编辑:程序博客网 时间:2024/05/28 05:14
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
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
【差分约束的裸题,直接跑最短路,不过本题时限较小,所以要将队列换成栈才不会T】
【差分约束,也是线性规划里的一种,是用来求不等式组的变量的最大取值。比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图:
由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了 】
【通过这样的解释,可以发现不等式的求解与最短路的求解有一定关系,即通过求最短路可以求出不等式的最大值】
【然而有的时候,不一定是要求最大值,当求最小值时,求最大路径。】
【注意:每次求解时,必须要将不等式化成a-b>=k或a-b<=k的情况,当出现用'>'或'<'连接时,改成a-b<=k-1或a-b>=k+1】
【若权值有负数,就要先判有没有负环,有负环的话就不存在结果】
#include<stack>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;stack<int>que;int a[150010],next[150010],p[150010],val[150010],tot;int dis[150010];int n,m;bool b[150010];inline void add(int x,int y,int z){ tot++; a[tot]=y; next[tot]=p[x]; p[x]=tot; val[tot]=z; return;}inline void spfa(int s,int e) { memset(dis,127/3,sizeof(dis)); memset(b,true,sizeof(b)); que.push(s); b[s]=false; dis[s]=0; while (!que.empty()) { int u,v; u=que.top(); que.pop(); v=p[u]; b[u]=true; while (v) { if (dis[a[v]]>dis[u]+val[v]) { dis[a[v]]=dis[u]+val[v]; if(b[a[v]]) { b[a[v]]=false; que.push(a[v]); } } v=next[v]; } } printf("%d\n",dis[e]); return;}int main() { int i; scanf("%d%d",&n,&m); for (i=1;i<=m;++i) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); } spfa(1,n); return 0;}
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- POJ 3159 Candies【差分约束+SPFA】
- poj 3159 Candies(差分约束 spfa stack实现)
- POJ 3159 Candies(SPFA+栈)差分约束
- POJ 3159 Candies(spfa最短路,差分约束)
- POJ 3159 Candies(差分约束+SPFA )
- 【poj 3159】Candies 差分约束+spfa(stack)
- 【POJ 3159】Candies(差分约束+SPFA)
- Candies+差分约束+spfa+poj
- poj Candies(差分约束,spfa栈)
- poj 3259 Candies spfa 差分约束
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- POJ 3159 Candies 差分约束 spfa+栈+邻接表
- POJ 3159 Candies(差分约束基础题,栈式SPFA)
- POJ 3159 Candies 差分约束-SPFA栈实现
- poj 3159 Candies(差分约束)
- POJ 3159Candies (差分约束)
- LeetCode 83. Remove Duplicates from Sorted List
- 二叉树之搜索树
- 删除数组中的元素(链表)
- Android xml绘图(四)-----------Selector
- 【BZOJ 1076】 [SCOI2008]奖励关 期望dp
- 【POJ 3159】Candies(差分约束+SPFA)
- linux命令: ps、grep、kill
- HDU 5976 逆元
- Python装饰器
- Linux下vim的配置
- 字符串处理函数、字符操作函数
- Kendall tau距离:求两个排列之间的逆序数
- 查看隐藏文件
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException Write operations are not allowed in