Candies POJ - 3159 (最短路+差分约束)
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
Hint
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; int dist[];
bool vis[]; typedef pair<int,int>pii;
struct Node
{
int y,val,next;
}node[]; int cnt,head[]; void add(int u,int v,int val)
{
node[++cnt].y=v;
node[cnt].val=val;
node[cnt].next=head[u];
head[u]=cnt;
} int n,m;
priority_queue<pii,vector<pii>,greater<pii> >que;
void dijstra()
{
while(!que.empty())que.pop();
que.push(pii(,));
memset(dist,,sizeof(dist));
while(!que.empty())
{
pii tmp = que.top();
que.pop();
int s=tmp.second;
int v=tmp.first;
if(vis[s])continue;
vis[s]=;
dist[s]=v;
for(int i=head[s];i;i=node[i].next)
{
int to = node[i].y;
if(dist[to] < v+node[i].val)
{
que.push(pii(v+node[i].val,to));
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
add(u,v,k);
}
dijstra();
printf("%d\n",dist[n]);
}
Candies POJ - 3159 (最短路+差分约束)的更多相关文章
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...
- K - Candies(最短路+差分约束)
题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
- poj 1364 King(差分约束)
题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...
- poj 1201 Intervals(差分约束)
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
随机推荐
- VS打开项目 提示Asp.net4.0未在web服务器上注册的解决方案
Visual Studio2013 下载的Visual Studio2013更新4 http://go.microsoft.com/fwlink/?LinkId=510314有关的Visual Stu ...
- Python——安居客租房信息爬取(以南昌为例)
前言: 提前安装好所需要的库. 本代码的输入仅需要某个城市的租房地址首页即可,其他自会生成. 使用前请创建所需的目录,或者为代码添加os.makedir() 支持断点重爬,重行运行即可. header ...
- python双端队列-collection模块
双端队列(double-ended queue,或者称deque)在需要按照元素增加的顺序来移除元素时非常有用.其中collection模块,包括deque类型. 使用实例:
- windows bat 批处理 执行 for 循环无法执行?
示例: cmd 命令行可以执行.但是 写成 bat 却不能执行, for /f "delims==" %a in ('dir /b /s F:\F\*.TXT')do copy ...
- Arduino常用的数据类型以及转换
常用的数据类型有布尔类型.字符型.字节型.整型.无符号整型.长整型.无符号长整型.浮点型.双精度浮点型等 布尔类型bollean: 布尔值是一种逻辑值,其结果只能为真(true)或者假(false). ...
- 移动端-处理后台传过来的html中图片的显示
function DealWithImg() { var width = 0; if (window.screen.width) { width = window.screen.width; } el ...
- Ubuntu18.04环境下melodic安装gmapping
Ubuntu18.04 环境下melodic中很多包没有提供sudo apt install的安装方式,需要通过源代码安装,安装方法如下: 1.先安装依赖库: sudo apt--dev sudo a ...
- BUGKU login3
先看的wp,呢么来复现一遍,emmmmmm,尝试一波,用户名输入admin后,密码随便输,发现提示password error,呢么填其他用户名的话,发现提示username does not exi ...
- 工程师的URL大全
作者:邓聪聪 https://bgp.he.net/ip #关于IP地址广播的AS查询及地址所属 http://ipwhois.cnnic.net.cn/ ...
- Ubuntu版本linux系统安装git
可以使用apt-get方式安装,也可以下载源代码安装,我们这里使用apt-git安装.但由于直接使用 sudo apt-get install git 安装的版本较老,因此我们参考[2]中给出的PPA ...