【poj3159】 Candies
http://poj.org/problem?id=3159 (题目链接)
题意
有n个小朋友,班长要给每个小朋友发糖果。m种限制条件,小朋友A不允许小朋友B比自己多C个糖果。问第n个小朋友最多比第1个小朋友多多少糖果。
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 Dijkstra() {
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);
}
Dijkstra();
printf("%d",dis[n]);
return 0;
}
【poj3159】 Candies的更多相关文章
- 【POJ3159】Candies 裸的pqspfa模版题
不多说了.就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include & ...
- 【POJ3159】Candies(差分约束系统)
题意:有一些人, 给n个人派糖果,给出m组约束,每组约束包含A,B,c 三个数, 意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c . 最后求n 比 1 最多多多少糖果 ...
- 【CF1063D】Candies for Children 数学
题目大意 有 \(n\) 个人排成一个圈,你有 \(k\) 颗糖,你要从第 \(l\) 个人开始发糖,直到第 \(r\) 个人拿走最后一颗糖.注意这 \(n\) 个人拍成了一个圈,所以第 \(n\) ...
- 【POJ2886】【线段树】Who Gets the Most Candies?
Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...
- HackerRank - candies 【贪心】
HackerRank - candies [贪心] Description Alice is a kindergarten teacher. She wants to give some candie ...
- 【POJ 3159】 Candies
[题目链接] 点击打开链接 [算法] 差分约束系统 [代码] #include <algorithm> #include <bitset> #include <cctyp ...
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【ACM】那些年,我们挖(WA)过的最短路
不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...
- Codeforces Round #428 A. Arya and Bran【模拟】
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- 2D Tookit (一) 精灵切割
Sprite Dicing 精灵切割 图一:原图 Diced 设置 Diced[切割]对比图 文档 http://www.2dtoolkit.com/docs/latest/advanced/sp ...
- Unity 协程与线程
协程是不同步的 协程 不是 线程,协同程序是 不同步 的 一个线程在程序中和其他线程是异步运行的,在多处理器机器中一个线程可以同时与所有其他线程的实时运行其代码,这使得线程编程能够解决很复杂的事情,因 ...
- Unity-WIKI 之 DebugConsole
功能预览 使用说明 1.把 DebugConsole.cs 放在 Standard Assets 目录下(重要) 2.如果你想修改 DebugConsole的一些参数,把DebugConsole.cs ...
- Linux+Mono+Asp.net入门:05CentOs安装Mono(上)
http://www.cnblogs.com/jameszou/archive/2013/05/18/3085754.html 准备工作 常见问题 a. Xshell显示中文乱码问题 [文件]–> ...
- 反序列化存入数据库里面的session数据
session数据存取的方法可通过session.serialize_handler方法来判断,反序列化可通过下面的unserialize方法,参考http://stackoverflow.com/q ...
- java 15- 5 List集合
需求 1:List集合存储字符串并遍历.(步骤跟Collection集合一样,只是最初创建集合对象中的集合类改变了,Collection变成List) List集合的特点: 有序(存储和取出的元素一致 ...
- UINavagationController
如何防止navigation多次push一个页面?有时候网慢,点了一下没反应,用户可能就多点几下,这时候会打开好几个一样的页面: 写了一个navigation基类,重写了push方法:传进来要push ...
- 【转】【C#】C#性能优化总结
1. C#语言方面 1.1 垃圾回收 垃圾回收解放了手工管理对象的工作,提高了程序的健壮性,但副作用就是程序代码可能对于对象创建变得随意. 1.1.1 避免不必要的对象创 ...
- 【MFC】ID命名和数字约定
ID命名和数字约定 MFC ID 命名和数字约定需要满足以下要求: 提供对 Visual C++ 资源编辑器支持的 MFC 库和 MFC 应用程序中使用的一致的 ID 命名标准. 这样就可以轻松地对程 ...
- INADDR_ANY的确切含义
INADDR_ANY就是inet_addr("0.0.0.0") 首先,需要明确的是当服务器的监听地址是INADDR_ANY时设置的是服务器的IP地址. 其次,当服务器的监听地址是 ...