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

题意给你n个熊孩子,现在给熊孩子们分配蜡烛,这n个熊孩子会提出m个要求,每个要求由a,b,c三个整数表示,意思是b孩子的蜡烛最多不能比a孩子多c个。

问你最后满足这样的要求后,第一个孩子和最后一个孩子最多会多拿多少个蜡烛。

这个提一开始想复杂了,首先意识到的就是松弛操作和这个题的描述差不多。
d[b]-d[a]<=dis[a,b]与松弛操作if (d[b]>d[a]+dis[a,b] d[b]=d[a]+dis[a,b]一样!就是在a,b两点之间建一条长度为后来看了大家的题解说spfa+queue会超时,只能手写堆栈。

代码如下

 #include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
const int Maxn=;
const int Maxe=;
const int inf =0x3f3f3f3f;
int n,m;
int tot;
int head[Maxn];
bool vis[Maxn];
int d[Maxn];
int Q[Maxn];
struct Edge
{
int to;
int dis;
int nxt;
}edge[Maxe];
void add (int a,int b,int c)
{
edge[tot].to=b;
edge[tot].dis=c;
edge[tot].nxt=head[a];
head[a]=tot++;
}
void spfa (int start,int n)
{
int top=;
for (int v=;v<=n;++v){
if (v==start){
Q[top++]=v;
vis[v]=true;
d[v]=;
}
else{
vis[v]=false;
d[v]=inf;
}
}
while (top!=){
int u=Q[--top];//top是盖子的位置
vis[u]=false;
for (int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if (d[v]>d[u]+edge[i].dis){
d[v]=d[u]+edge[i].dis;
if (!vis[v]){
vis[v]=true;
Q[top++]=v;
}
}
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&m)){
tot=;
memset(head,-,sizeof head);
for (int i=;i<m;++i){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
spfa(,n);
printf("%d\n",d[n]);
}
return ;
}

最新文章

  1. Windows Programming ---- Beginning Visual C#
  2. npm不是以管理身份运行遇到的问题
  3. [ruby on rails] 跟我学之(8)修改数据
  4. MySql之on duplicate key update详解
  5. 慕课网-安卓工程师初养成-2-11 Java常量
  6. oracle PL/SQL(procedure language/SQL)程序设计--控制结构(if else )
  7. ListView添加项目带序列
  8. Python Quick Start
  9. 【Java】环境变量的配置
  10. vim搜索后跳到下(上)一个
  11. Wireless Network(POJ 2236)
  12. 解决open-vm-tools安装时Failed to get unit file state for run-vmblockx2dfuse.mount
  13. 安全扫描工具-AppScan
  14. ExtJs的Ext.Ajax.request实现waitMsg等待提示效果
  15. 推荐多线程下载工具axel替代wget
  16. HDOJ 5639 Transform
  17. docker完整配置nginx+php+mysql
  18. mysql-语法大全
  19. Visual Studio 2015编译wxWidgets
  20. 团队作业——Alpha冲刺 1/12

热门文章

  1. Python 中内建属性 __getattribute__
  2. Security基础(三):OpenSSL及证书服务、邮件TLS/SSL加密通信
  3. AcWing 248. 窗内的星星 (扫描线)打卡
  4. php 封装原生数据导出的方法(csv文件格式)和csv文件中长数字自动变成科学计数法的处理
  5. JDK1.8中ArrayList的实现原理及源码分析
  6. Archive.org:互联网档案馆
  7. idea中以maven工程的方式运行tomcat源码
  8. Python中多线程的阻塞问题
  9. QTP 通过URL地址下载文件到本地(转)
  10. 添加阿里巴巴图标,让你页面小图标都是CSS3写成