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 AB and cin 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.

差分约束的经典题目

意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c

就是把不等式关系转换成最短路里面的松弛条件

点很多 又是稀疏矩阵 所以用邻接表来存 head相当于头指针 next[i]记得是第i条边连接的下一条边的编号

用了spfa 如果用STL里的queue的话会T

要改用数组表示栈

还有就是 用cin cout也会T

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#define inf 0x3f3f3f3f using namespace std; int n, m, num_edge;
struct{
int to, v, nnext;
}edge[150005];
int dis[30005], head[30005], Q[30005];
bool vis[30005]; void addedge(int a, int b, int c)
{
edge[num_edge].to = b;
edge[num_edge].v = c;
edge[num_edge].nnext = head[a];
head[a] = num_edge++;
} void spfa(int sec)
{
int top = 0;
for(int v = 1; v <= n; v++){
if(v == sec){
Q[top++] = v;
vis[v] = true;
dis[v] = 0;
}
else{
vis[v] = false;
dis[v] = inf;
}
}
while(top){
int u = Q[--top];
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].nnext){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].v){
dis[v] = dis[u] + edge[i].v;
if(!vis[v]){
vis[v] = true;
Q[top++] = v;
}
}
}
}
} int main()
{
while(cin>>n>>m){
num_edge = 0;
for(int i = 1; i <= n; i++){
head[i] = -1;
}
for(int i = 0; i < m; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c);
}
spfa(1);
printf("%d\n", dis[n]);
}
return 0;
}

POJ3150 Candies【差分约束】的更多相关文章

  1. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  2. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  3. POJ 3159 Candies 差分约束dij

    分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...

  4. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  5. [poj3159]Candies(差分约束+链式前向星dijkstra模板)

    题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...

  6. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  7. poj3159 Candies(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit ...

  8. POJ3159 Candies —— 差分约束 spfa

    题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submiss ...

  9. Candies(差分约束)

    http://poj.org/problem?id=3159 题意: flymouse是幼稚园班上的班长,一天老师给小朋友们买了一堆的糖果,由flymouse来分发,在班上,flymouse和snoo ...

随机推荐

  1. 九度 1254:N皇后问题

    Leetcode 原题. 这里 N 最大会取到 13, TLE 了 代码 #include <iostream> #include <stdio.h> using namesp ...

  2. ios开发周期之--(向上,向下,四舍五入)取整

    ceil(x)返回不小于x的最小整数值(然后转换为double型). floor(x)返回不大于x的最大整数值. round(x)返回x的四舍五入整数值.

  3. centos7安装python-3.5

    sudo yum install gcc wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz sudo cp Python-.t ...

  4. redis 列表

    Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967 ...

  5. Oracle查询锁表和解锁

    1.查询是否锁表 SELECT l.session_id sid, s.serial#, l.locked_mode,l.oracle_username, l.os_user_name,s.machi ...

  6. Kafka(一)-- 初体验

    一.概念 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键因素. 这些 ...

  7. C# Serializable对象序列化的作用

    http://www.cnblogs.com/linlf03/archive/2011/11/03/2234424.html 1.序列化定义:将对象转换为容易传输的格式的过程.例如,可以序列化一个对象 ...

  8. inux跟踪线程的方法:LWP和strace命令

    摘要:在使用多线程程序时,有时会遇到程序功能异常的情况,而这种异常情况并不是每次都发生,很难模拟出来.这时就需要运用在程序运行时跟踪线程的手段,而linux系统的LWP和strace命令正是这种技术手 ...

  9. 教你在windows下安装使用配置vim+gcc[转]

    转自http://blog.163.com/lixiangqiu_9202/blog/static/535750372012461190722/ 一直在使用linux,但有时也会去虚拟机里的winxp ...

  10. 《Lua程序设计》第6章 深入函数 学习笔记

    在Lua中,函数是一种“第一类值(First-Class Value)”,它们具有特定的词法域(Lexical Scoping).“词法域”:函数可以潜逃在另一个函数中,内部的函数可以访问外部函数中的 ...