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. hdu 2435dinic算法模板+最小割性质

    hdu2435最大流最小割 2014-03-22 我来说两句 来源:hdu2435最大流最小割 收藏 我要投稿 2435 There is a war 题意: 给你一个有向图,其中可以有一条边是无敌的 ...

  2. “XXX.Index”不扩展类“System.Web.UI.Page”,因此此处不同意的问题

    "XXX.Index"不扩展类"System.Web.UI.Page",因此此处不同意的问题 原因:设计页面继承的路径和后台.cs页面类的路径不一致造成的 看下 ...

  3. angularjs1-路由

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 4.QList

    #include "mainwindow.h" #include <QApplication> #include <QLabel> #include < ...

  5. 编译libvlc。。。

    https://wiki.videolan.org/Win32Compile按照官网教程,安装所需工具,参考 :http://qjw.qiujinwu.com/blog/2014/12/08/cros ...

  6. Ubuntu14.04下Mongodb的Java API编程实例(手动项目或者maven项目)

    不多说,直接上干货! 若大家,不会安装的话,则请移步,随便挑选一种. Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐) Ubuntu14.04 ...

  7. Kafka.net使用编程入门

    最近研究分布式消息队列,分享下! 首先zookeeper  和 kafka 压缩包 解压 并配置好! 我本机zookeeper环境配置如下: D:\Worksoftware\ApacheZookeep ...

  8. Android Finalizing a Cursor that has not been deactivated or closed

    问题描述: 使用Sqlite数据库时,有时候会报下面的异常: Finalizing a Cursor that has not been deactivated or closed 一个光标没有被停用 ...

  9. .NET XML POST 请求

    //请求体,XML参数 string xmlstring = @"<root></root>“; //请求URL string postUrl ="http ...

  10. ListView的Item动画

    1.效果图 2.需求就是点击item歌曲时,实现一种飞入到预约按钮处的效果 3.思路:在布局文件中加入了一个条目布局,和listView的item一样,点击listView的item时,使用给条目布局 ...