Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 27051   Accepted: 7454

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 overc 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个孩子,再给出M个限制。A,B,c表示孩子B的糖果最多比孩子A的糖果多c个。问N号孩子最多比1号孩子多多少的糖果。
思路:转化为求1号结点到N号结点的最短路问题。
/*
dijkstra Accepted 3112KB 547ms G++
*/
#include"cstdio"
#include"cstring"
#include"queue"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
struct P{
int fi,se;
P(int cfi,int cse):fi(cfi),se(cse){}
bool operator<(const P& a) const
{
return fi > a.fi;
}
};
int heap[MAXN];
int V,E;
void add_edge(int u,int v,int co)
{
es[E].to=v;
es[E].cost=co;
es[E].next=heap[u];
heap[u]=E;
E++;
}
int d[MAXN];
int dijkstra(int s)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P> que;
que.push(P(,s));
d[s]=;
while(!que.empty())
{
P p=que.top();que.pop();
int v=p.se;
if(d[v]<p.fi) continue;
for(int i=heap[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[v]+e.cost)
{ d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
return d[V];
}
int main()
{ int N,M;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(heap,-,sizeof(heap));
V=N,E=;
for(int i=;i<M;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
}
int ans=dijkstra();
printf("%d\n",ans);
} return ;
}

下面是用栈实现的spfa算法。用队列实现会TLE。

/*
spfa Accepted 3112KB 547ms G++
*/
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
int stack[MAXN],top;
int head[MAXN];
int V,E;
void add_edge(int u,int v,int co)
{
es[E].to=v;
es[E].cost=co;
es[E].next=head[u];
head[u]=E;
E++;
}
int d[MAXN];
int vis[MAXN];
int spfa(int s)
{
for(int i=;i<=V;i++) d[i]=INF;
memset(vis,,sizeof(vis));
top=;
stack[top++]=s;
vis[]=s,d[s]=; while(top!=)
{
int v=stack[--top];
vis[v]=;
for(int i=head[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
if(!vis[e.to])
{
vis[e.to]=;
stack[top++]=e.to;
}
}
}
}
return d[V];
}
int main()
{ int N,M;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(head,-,sizeof(head));
V=N,E=;
for(int i=;i<M;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
}
int ans=spfa();
printf("%d\n",ans);
} return ;
}

最新文章

  1. MIT 6.828 JOS学习笔记11 Exercise 1.8
  2. char和byte的区别
  3. libgcc_s.so.1 must be installed for pthread_cancel to work
  4. Linux软raid创建
  5. SPOJ TSUM Triple Sums(FFT + 容斥)
  6. android-sdks/build-tools/17.0.0/aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
  7. Android HttpClient GET或者POST请求基本使用方法(转)
  8. 国内顺利使用Google的另类技巧
  9. MVC小系列(十三)【全局异常处理与异常日志】
  10. 浏览器标题栏添加小logo图片,记录一下,方便以后用
  11. POJ 3307 Smart Sister
  12. [boost] build boost with intel compiler 16.0.XXX
  13. iOS中 快速正确的安装 CocoaPods
  14. SpringBoot开发案例从0到1构建分布式秒杀系统
  15. flask blueprint
  16. Spring Security OAuth笔记
  17. git android.google 源码:Unknown SSL protocol error in connection to code.google.com:443
  18. (转)一次压测对nginx/tomcat配置的调整
  19. MyEclipse中使用Junit插件进行单元测试
  20. idea tomcat添加

热门文章

  1. uva 11404 dp
  2. Redis源码试读(一)源码准备
  3. HDFS源码分析数据块复制监控线程ReplicationMonitor(一)
  4. linux SPI驱动——spi core(四)
  5. 14 nginx 中配置 expires缓存提升网站负载
  6. 用Newtonsoft将json串转为对象的方法(详解)
  7. 【BZOJ4653】[Noi2016]区间 双指针法+线段树
  8. iOS10.3 UILable中划线失效问题
  9. Moving Computation is Cheaper than Moving Data
  10. JAVA 水果机游戏及编码