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(){;}

最新文章

  1. 使用CSS使内容垂直居中的N中方法。
  2. vim中大小写转换
  3. pull刷新
  4. 64位WIN7+oracle11g+plsql安装
  5. PHP 防范CC攻击
  6. C# 消息处理机制及自定义过滤方式
  7. mysql 查询多个id
  8. uboot相关命令及用法
  9. [数字dp] hdu 3565 Bi-peak Number
  10. Nodejs --我自己的学习笔记
  11. BITMAP图片压缩算法三则--bilinear、nearest、cubic
  12. (转)CSS颜色及&lt;a&gt;标签超链接颜色改变
  13. Spring Data Jpa简单了解
  14. git stash解决代码merge出错
  15. Java设计模式之原型设计模式
  16. C# 分支语句 练习题(中间变量,随机数)
  17. Flutter - 创建自适应的Android app 图标
  18. 如何安装win10和linux [ubuntu14]双系统
  19. cocos2dx 不同平台上加载文件
  20. web07-jdbcBookStore

热门文章

  1. L - 还是畅通工程
  2. 设计模式之二十:责任链模式(Chain of Responsibility)
  3. Windows平台编译openssl-0.9.8k库(32位、64位)
  4. Linux 网卡驱动学习(六)(应用层、tcp 层、ip 层、设备层和驱动层作用解析)
  5. Universal-Image-Loader(UIL)图片载入框架使用简介
  6. 英语发音规则---D字母
  7. CodeForces--609C --Load Balancing(水题)
  8. Python的四个内置数据类型list, tuple, dict, set
  9. Mediator 基于内存的发布订阅
  10. vue 父子组件通信props/emit