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 A, B 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

分析题意:属于单源最短路
dijkstra+优先队列 代码如下
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;
struct node
{
int e;
int w;
bool friend operator <(node s1,node s2)
{
return s1.w>s2.w;
}
};
vector<vector<node> > edge;
priority_queue<node>q;
bool beused[30005];
node ww;
int main()
{
int n,m;
int A,B,C;
scanf("%d%d",&n,&m);
edge.clear();
edge.resize(n+1);
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&A,&B,&C);
ww.e=B;
ww.w=C;
edge[A].push_back(ww);
}
memset(beused,0,sizeof(beused));
while(!q.empty())
q.pop();
ww.e=1;
ww.w=0;
q.push(ww);
while(!q.empty())
{
ww=q.top();
q.pop();
if(beused[ww.e])
continue;
beused[ww.e]=true;
if(ww.e==n)
break;
int j=edge[ww.e].size();
for(int i=0;i<j;i++)
{
node r;
r.e=edge[ww.e][i].e;
if(beused[r.e])
continue;
r.w=ww.w+edge[ww.e][i].w;
if(!beused[r.e])
q.push(r);
}
}
printf("%d\n",ww.w);
return 0;
}
 

poj 3159 dijkstra 最短路的更多相关文章

  1. Candies POJ - 3159 (最短路+差分约束)

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

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

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

  3. POJ 1161 Walls(最短路+枚举)

    POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...

  4. Dijkstra最短路算法

    Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...

  5. dijkstra(最短路)和Prim(最小生成树)下的堆优化

    dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...

  6. 【坐在马桶上看算法】算法7:Dijkstra最短路算法

           上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径 ...

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

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

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

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

  9. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

随机推荐

  1. 有关嵌入式linux的注意点总结

    知识收集和个人学习过程遇到的问题. 仅供参考. 1.sudo apt-get update 一直无法更新 一,查看网络是否连接上 有几种网络连接方式.常用的两种有网桥网络(Bridged)和网络地址翻 ...

  2. Rhel6-lvs配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 相关网址:http://zh.linuxvirtualserver.org/ yum仓库配置: [rh ...

  3. SharePoint 2013 Nintex Workflow 工作流帮助(五)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 4. Assign To-Do Task(User interaction分组) 直观理解,指派待办任务给一个或多个用户 ...

  4. Visual Studio 2013 如何关闭调试而不关闭IIS Express

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就OK了 (英文版的请对应相应的操作) 不过这是针对所有的调试,如果你想针 ...

  5. 2014年3月份第1周51Aspx源码发布详情

    Graphics创建饼图示例源码  2014-3-7 [VS2010]源码描述:这个程序是一个在c#中使用图形类用来创建饼图,此程序是用Graphics 类的DrawPie() 和 FillPie() ...

  6. [TOP10]十大渗透测试演练系统

    本文总结了目前网络上比较流行的渗透测试演练系统,这些系统里面都提供了一些实际的安全漏洞,排名不分先后,各位安全测试人员可以亲身实践如何利用这个漏洞,同时也可以学习到漏洞的相关知识. DVWA (Dam ...

  7. (转) function与感叹号

    原文:http://blog.sina.com.cn/s/blog_775f158f01016j12.html function与感叹号(转)(2012-08-29 12:29:12) 最近有空可以让 ...

  8. hd oj2015

    #include "stdio.h"int main(){ int n,m,k,num,i,sum=0; while(~scanf("%d%d",&n, ...

  9. 搭建EJB3开发环境

    开发工具:myeclipse8.5.antjdk:1.5容器:jboss4.2.31.安装jboss:解压.配置JBOSS_HOME环境变量2.测试安装是否成功:启动%JBOSS_HOME%\bin\ ...

  10. 控制台应用程序中Main函数的args参数

    在VS中添加参数 菜单   项目   --   你的项目属性   --   调试   --   启动选项   --   命令行参数 参数之间用空格分隔开就可以了,如果参数有空格,以双引号风格