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

32-bit signed integer type is capable of doing all arithmetic.
 
题意:求1到n的最大路径
思路:将每个人允许的最大糖果差当作路径,然后求最短路,因为到每个点,要满足每个人到这个点的最大糖果差,所以选择最小的糖果数肯定满足所有情况
(差分约束?)
 #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 (最短路+差分约束)的更多相关文章

  1. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  2. POJ 3169 Layout(差分约束+最短路)题解

    题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...

  3. K - Candies(最短路+差分约束)

    题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...

  4. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  5. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  6. poj 1364 King(差分约束)

    题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...

  7. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  8. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  9. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

随机推荐

  1. GIT-windows系统部署gitblit服务器

    windows系统部署 gitblit 服务器 1. 安装JAVA环境 下载Java,下载地址:http://www.java.com/zh_CN/ 安装Java.安装步骤不再详述.      配置J ...

  2. 人工智能初识(百度ai)

    目前的人工智能做了什么? 语音识别:小米的小爱同学,苹果的siri,微软的Cortana语音合成:小米的小爱同学,苹果的siri,微软的Cortana图像识别:交通摄像头拍违章,刷脸解锁手机等视频识别 ...

  3. PMP知识点(三)——挣值计算汇总表

    在新标签页打开. 附参考图 资料地址:http://pan.baidu.com/s/1bMNroq

  4. QMQTT简单介绍(2)

    QMQTT mqtt client for Qt Please compile the library with Qt >= 5.3 version. On Windows you need t ...

  5. svn 解决冲突

    当svn update时提示如下: D C main.go > local file unversioned, incoming file add upon updateSummary of c ...

  6. AppCan

    启动服务 将app程序寄宿在计算机上,在计算机上调试:访问服务地址,将appToken值复制一下 在浏览器输入192.168.2.102:3000/appToken的值/文件路径后即可调试 入口文件 ...

  7. 2018-2019 20165237网络对抗 Exp4 恶意代码分析

    2018-2019 20165237网络对抗 Exp4 恶意代码分析 实验目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后 ...

  8. Message高级特性 & 内嵌Jetty实现文件服务器

    1. Messaage Properties  常见属性 更多的属性以及介绍参考:http://activemq.apache.org/activemq-message-properties.html ...

  9. SqlServer存储过程及函数

    存储过程和函数类似于Java中的方法. ⒈存储过程 一组预先编译好的sql语句的集合,理解成批处理语句. 好处: ①提高代码的重用性 ②简化操作 ③减少了编译次数并且减少了和数据库服务器的连接次数,提 ...

  10. day08 文件操作

    1.三种字符串: (1)u'' 普通字符串 ---> u'abc' ---> 默认的文本方式,以字符作为文本的输出方式 (2)b'' 二进制字符串 ---> b'ASCII码' -- ...