http://poj.org/problem?id=3159

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 33328   Accepted: 9349

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

Source

 
题意:n个人,m个关系,a b c 表示 a最多比b少c 即 d[b]-d[a]<=c—>>d[b]<=d[a]+c
SPFA有队列会超时、、、、呃呃呃
 #include <cstring>
#include <cstdio>
#include <stack> const int N(+);
const int M(+);
int n,m,u,v,w;
int head[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[M];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} int dis[N];
bool instack[N];
int SPFA(int s)
{
for(int i=;i<=n;i++)
instack[i]=,dis[i]=0x7fffffff;
dis[s]=instack[s]=;
std::stack<int>sta; sta.push(s);
for(;!sta.empty();)
{
u=sta.top(); sta.pop(); instack[u]=;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!instack[v]) instack[v]=,sta.push(v);
}
}
}
return dis[n];
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
for(;~scanf("%d%d",&n,&m);)
{
for(;m--;ins(u,v,w))
read(u),read(v),read(w);
printf("%d\n",SPFA());
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
sumedge=;
}
return ;
} int Hope=AC();
int main(){;}

POJ——T 3159 Candies的更多相关文章

  1. (poj)3159 Candies

    题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...

  2. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  3. poj 3159 Candies (dij + heap)

    3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...

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

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

  5. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  6. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  7. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  8. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  9. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

随机推荐

  1. BA-给排水-供水系统自动控制(转载)

    浙江省建筑设计研究院划 杨绍胤 杨庆 摘 要:探讨供水系统变流量和恒压自动控制和设计方法.关键词: 供水系统 自动控制 传统给水系统常在屋顶设置高位水箱.水从地下水箱用水泵打到高位水箱.从高位水箱通过 ...

  2. Android APP漏洞挖掘

    0x00 1.组件公开安全漏洞 參考Android 组件安全. 2.Content Provider文件文件夹遍历漏洞 參考Content Provider文件文件夹遍历漏洞浅析. 3.Android ...

  3. 小米2S电池电量用尽充电无法开机解决方法

    背景:        昨晚睡觉前关机,记得电量还有百分之七八十,但早上起床后,指示灯一直红灯闪烁.按开机键和其它键都没反应! ! 解决方法:         扣下电池,用万能充冲电,略微多冲一会,由于 ...

  4. [Android]RecyclerView的简单演示样例

    去年google的IO上就展示了一个新的ListView.它就是RecyclerView. 下面是官方的说明,我英语能力有限,只是我大概这么理解:RecyclerView会比ListView更具有拓展 ...

  5. 深刻理解Nginx之Nginx完整安装

    1.   Nginx安装 1.1预先准备 CentOS系统下,安装Nginx的库包依赖. 安装命令例如以下: sudo yum groupinstall "DevelopmentTools& ...

  6. MFC窗口去边框、置顶、全屏、激活

    静态移除长提边框非常easy,直接设置"Border"属性为"none"就可以 "Maximize Box", "Minimize ...

  7. 新东方雅思词汇---6.1、oppose

    新东方雅思词汇---6.1.oppose 一.总结 一句话总结:op(相反)+pos(放) 英 [ə'pəʊz]  美 [ə'poz]  vt. 反对:对抗,抗争 vi. 反对 [ 过去式 oppos ...

  8. php后期静态绑定

    php后期静态绑定 自 PHP 5.3.0 起,PHP 增加了一个叫做后期静态绑定的功能,用于在继承范围内引用静态调用的类. 虽然也可以调用非静态方法,但是不会在运行时绑定. static 不再只是简 ...

  9. NPAPI——实现非IE浏览器的类似ActiveX的本地程序(插件)调用

    一.Netscape Plugin Interface(NPAPI) 大致的说明可以看下官方文档Plugin 本文主要针对于JavaScript与插件交互部分做一些交流,比如用于数字证书的操作(淘宝和 ...

  10. Laravel-事件简单使用

    Laravel-事件简单使用 标签(空格分隔): php, laravel 注册事件和监听器 生成事件和监听器:php artisan event:generate key => 事件 valu ...