Candies

Time Limit: 1500MS Memory Limit: 131072K

Description

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.

题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。最后求小孩中的最大差值是多少。

题解:这是一题典型的差分约束题,可以把糖果看成距离,然后就是求最短路的问题了。

由 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。

坑点就在于SPFA队列会超时,所以采取堆栈。

#include <iostream>
#include <cstdio> using namespace std; const int maxn = 30050;
const int INF = 1e9+7; int q[maxn],head[maxn],f[maxn],dis[maxn];
int num,n; struct node
{
int to,next,w;
}edge[150050]; void add(int v,int u,int w)
{
edge[num].to = u;
edge[num].next = head[v];
edge[num].w = w;
head[v] = num++;
} void SPFA()
{
int i,top;
for(i=1;i<=n;i++)
{
f[i] = 0;
dis[i] = INF;
}
top = 0;
dis[1] = 0;
f[1] = 1;
q[top++] = 1;
while(top)
{
int u,v,w;
u = q[top-1];
f[u] = 0;
top--;
for(i=head[u];i!=-1;i=edge[i].next)
{
v = edge[i].to;
w = edge[i].w;
if(dis[u]+w<dis[v])
{
dis[v] = dis[u] + w;
if(!f[v])
{
f[v] = 1;
q[top++] = v; }
}
}
}
cout<<dis[n]<<endl;
} int main()
{
int m,i,a,b,c;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
head[i] = -1;
num = 0;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
SPFA();
return 0;
}

POJ-3159_Candies的更多相关文章

  1. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  2. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  8. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  9. poj 2352 Stars 数星星 详解

    题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...

  10. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

随机推荐

  1. warning: deprecated conversion from string constant to 'char*

    warning: deprecated conversion from string constant to 'char* #include<iostream> using namespa ...

  2. MySQLDemo2

    -- 查询所有数据库 show databases -- 删除数据库 drop database a -- use `数据库名称`; 表示使用此数据库 use mybatis -- 查看表结构 sho ...

  3. Pull Request的过程、基于git做的协同开发、git常见的一些命令、git实现代码的review、git实现版本的管理、gitlab、GitHub上为开源项目贡献代码

    前言: Pull Request的流程 1.fork 首先是找到自己想要pull request的项目, 然后点击fork按钮,此时就会在你的仓库中多出来一个仓库,格式是:自己的账户名/想要pull ...

  4. java-异常进阶-包的使用

    一 finally 1.1 异常执行的顺序 package test; public class Test { public static void main(String[] args) { Dem ...

  5. 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交

    P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...

  6. PHPStorm 批量选择,多光标同时编辑相同的内容

    一直按Alt+J

  7. golang之字符串

    字符串中的每一个元素叫做“字符”.在遍历或者单个获取字符串元素时可以获得字符.严格来说,这并不是 Go语言的一个类型,字符只是整数的特殊用例. (1)最后要注意,字符串值是不可变的.也就是说,我们一旦 ...

  8. HDU 3555 (递推&&记忆化)

    #include<stdio.h> #include<string.h> #define max 25 typedef __int64 LL; LL dp[max][]; // ...

  9. day38 16-Spring的Bean的装配:注解的方式

    Struts 2和hibernate也使用注解,但是使用注解在以后的开发中应用不多.但是可以说在整合的时候如何进行注解开发.在Spring中,注解必须会玩. package cn.itcast.spr ...

  10. HTTP之request请求(注册)

    HTTP之request请求 request:请求 作用:获取浏览器发送过来的数据 组成部分:请求行 请求头 请求体 操作请求行 格式: 请求方式 请求资源 协议/版本 常用方法:HttpServle ...