【poj3159】 Candies
来源:互联网 发布:淘宝店招商 编辑:程序博客网 时间:2024/05/01 08:30
http://poj.org/problem?id=3159 (题目链接)
题意:有n个小朋友,班长要给每个小朋友发糖果。m种限制条件,小朋友A不允许小朋友B比自己多C个糖果。问第1个小朋友最多比第n个小朋友多多少糖果。
Solution
原来这就是所谓的差分约束。。浅显易懂的博客,超详细的博客。
总结一下:
>=,求最小值,做最长路;
<=,求最大值,做最短路。
可能会觉得很奇怪,用线性规划的角度解释吧。其实我们需要求的就是(n)-(1)<=x或者(n)-(1)>=x,要保证满足所有的约束的话,我们需要求出最小(大)的x。所以就用最短路求出<=情况的最小x,用最长路求出>=情况的最大x。
还有就是有最短路负环(最长路正环)的话说明无解。答案为inf(-inf)时为任意解。
代码:
// poj3159#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#define LL long long#define inf 2147483640#define MOD 998244353#define Pi acos(-1.0)#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);using namespace std;inline LL getint() { int f,x=0;char ch=getchar(); while (ch<='0' || ch>'9') {if (ch=='-') f=-1;else f=1;ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}const int maxn=30010,maxm=150010;struct edge {int to,next,w;}e[maxm];struct data { int x,num; friend bool operator < (const data &a,const data &b) { return a.x>b.x; }};int dis[maxn],vis[maxn],head[maxn],cnt,n,m;priority_queue<data> q;void insert(int u,int v,int w) { e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;}void Dijistra() { data x,y; x.x=0;x.num=1; for (int i=1;i<=n;i++) dis[i]=inf; dis[1]=0; q.push(x); while (q.size()) { x=q.top();q.pop(); if (vis[x.num]) continue; vis[x.num]=1; for (int i=head[x.num];i;i=e[i].next) if (e[i].w+x.x<dis[e[i].to] && !vis[e[i].to]) { y.num=e[i].to; dis[e[i].to]=y.x=e[i].w+x.x; q.push(y); } }}int main() { scanf("%d%d",&n,&m); for (int u,v,w,i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); insert(u,v,w); } Dijistra(); printf("%d",dis[n]); return 0;}
0 0
- poj3159 Candies
- poj3159 Candies
- poj3159 Candies
- 【poj3159】 Candies
- [POJ3159]candies
- poj3159——Candies
- POJ3159——Candies
- poj3159 Candies(SPFA+stack)
- poj3159 candies dijkstra
- POJ3159-Candies-最短路
- poj3159--Candies(差分约束)
- Candies-POJ3159差分约束
- 差分约束 poj3159 Candies
- 【差分约束系统】Candies POJ3159
- 【POJ3159】Candies 裸的pqspfa模版题
- poj3159 Candies--单源最短路径&差分约束
- poj3159 Candies(差分约束入门)
- poj3159-Candies差分约束-spfa+栈
- 编程之美读书笔记-1的数目
- theano学习指南--深度置信网络(DBN)(源码)
- 【NOIP提高】看电影
- 【Java基础】——HashMap设计原理&实现分析
- C++实现strcpy, strlen, strstr, atoi等字符串相关API
- 【poj3159】 Candies
- 源代码——哈夫曼树的编码解码
- ChiMerge 算法: 以鸢尾花数据集为例
- myeclipse搭建maven web项目
- python自学历程
- 最小的K个数
- C++推箱子小游戏2.2(支持步数显示,回撤,穿墙,闯关,输入等功能)
- Python3.5学习之旅02
- 国家/地区语言代码(转)