【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 ...
随机推荐
- ArcGis 中MapControl 框选
void mCtrl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) ...
- 11Spring_AOP编程(AspectJ)_概述
AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut .对比前一种提到的传统的Aop编程,AspctJ更加的常用.Asp ...
- RDLC系列之五 初试XAML
本章只讲解xaml部分,其余都和winform下一样 1.xaml代码 <Window x:Class="RDLC.WPF.MainWindow" xmlns="h ...
- C语言 百炼成钢5
//题目13:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 //本身.例如:153是一个“水仙花数”,因为153 = 1的三次方+5的三次方+3的三次方. #de ...
- REST风格的原则
一个好的RESTful API,应该具备以下特征: 这个API应该是对浏览器友好的,能够很好地融入Web,而不是与Web格格不入. 浏览器是最常见和最通用的REST客户端.好的RESTful API应 ...
- OC格式化标准符
%@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X ...
- error C2065: “CMainFrame”: 未声明的标识符
xxxView.cp的开头包含 框架的头文件即可 : #include "MainFrm.h"
- Scrum敏捷精要
本文抽取Scrum中的一些重要思想和概念,对Scrum敏捷执行的主题流程进行精要的介绍. 一.基本思想 个体和互动 高于 流程和工具 工作的软件 高于 详尽的文档 客户合作 ...
- 360wifi 在 windows server 2008 / 2003 的使用方法
1. 安装驱动 在地址栏输入:Control Panel\System and Security\Administrative Tools , 然后找到Server Manager 打开 Server ...
- 最新微信小程序(应用号)视频教程,实战教程
1.1课程介绍,定个小目标 http://v.youku.com/v_show/id_XMTc2NzIwNDk1Ng==.html 1.2开发文档简读,了解全貌 ht ...