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. Exp1:PC平台逆向破解 20164314 郭浏聿

    一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getS ...

  2. jmeter使用csv传参进行并发测试验证

    1.获取到注册接口,添加HTTP信息头管理器.HTTP请求,设置好入参,且检查使用csv文件传参的入参 2.创建csv文件,写入需要传的入参 3.添加CSV Data Set Config 设置配置 ...

  3. Luogu P1082 同余方程(NOIP 2012) 题解报告

    题目传送门 [题目大意] 求关于x的同余方程 ax≡1(mod b)的最小整数解. [思路分析] 由同余方程的有关知识可得,ax≡1(mod b)可以化为ax+by=1,此方程有解当且仅当gcd(a, ...

  4. Mysql5.7数据导出提示--secure-file-priv选项问题的解决方法

    mysql可使用into outfile参数把表中的数据到处到csv,示例如下: select user_id from weibo_comment into outfile '/home/dazha ...

  5. ubuntu16.04 安装vscode

    1.通过官方PPA安装Ubuntu make sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update su ...

  6. gojs常用API-画布定义

    持续更新中 基础画布定义API画布初始位置 initialContentAlignment: go.Spot.Center,画布位置,定义后就不能拖动画布了,画布位置交由gojs管理 contentA ...

  7. 利用YOLOV3训练自己的数据

    写在前面:YOLOV3只有修改了源码才需要重新make,而且make之前要先make clean. 一.准备数据 在/darknet/VOCdevkit1下建立文件夹VOC2007. voc2007文 ...

  8. pycharm安装package时报错

    在pycharm pip 包时,提示报错:module 'pip' has no attribute 'main' 原因:由于我的是pip 18.1 版本里没有main() 解决方法: 如不降级 pi ...

  9. 24 类:组合 继承 super关键字 面向对象的三大性

    组合 组合:自定义类的对象作为另外一个类的属性 class Teacher: def __init__(self, name, age): self.name = name self.age = ag ...

  10. MQTT报文格式

    MQTT报文结构 控制报文由三部分组成: 1.Fixed header 固定报头,所有报文都包含 2.Variable header 可变报头,部分报文包含 3.Body 有效载荷,部分报文包含 固定 ...