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. goldengate 12c 针对oracle 12c配置的主要变化

    由于oracle 12c已经是多租户架构,在使用OGG同步的时候,需要考虑下面一些情况 一个 CDB包含多个PDB,源端部署的一个extract可访问所有pdb redo,理论上不需要每个pdb单独配 ...

  2. scanf

    scanf函数: (1)与printf函数一样,都被定义在头文件stdio.h里,因此在使用scanf函数时要加上#include <stdio.h>.它是格式输入函数,即按用户指定的格式 ...

  3. sqlserver定时备份

    前言:给客户部署好系统以后,如果不加一个定时备份,总感觉心里不放心,所以一定要做定时备份,并且定时备份是很简单的 新建作业--基本信息 新建步骤 ) ),) print @filename , NOF ...

  4. poj3356 dp

    //Accepted 4100 KB 0 ms //类似poj1080 //dp[i][j]表示s1用前i个,s2用前j个的最少匹配步数 //dp[i][j]=min(dp[i][j-1]+1,dp[ ...

  5. c#多线程(UI线程,控件显示更新) Invoke和BeginInvoke 区别

    如果只是直接使用子线程访问UI控件,直接看内容三,如果想深入了解从内容一看起. 一.Control.Invoke和BeginInvoke方法的区别 先上总结: Control.Invoke 方法 (D ...

  6. 知道创宇研发技能表v2.2

    知道创宇研发技能表v2.2 2014/3/9 发布 by @知道创宇(www.knownsec.com) @余弦 & 行之 知道创宇是国内Geek十足且普遍被认为特别有前途的互联网安全公司, ...

  7. 利用DetachedCriteria实现模糊查询和分页

      分类: Java-Developing  前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单 ...

  8. Ubuntu 14.10 下卸载MySQL

    前面讲了Mysql的简单安装方式,通过sudo apt-get install mysql-server 等脚本,安装之后如何卸载? 1 通过下面命令删除MySQL sudo apt-get auto ...

  9. 使用Wireshark捕捉USB通信数据

    USB,是英文Universal Serial Bus(通用串行总线)的缩写,而其中文简称为“通串线”,是一个外部总线标准,用于规范电脑与外部设备的连接和通讯.USB接口支持设备的即插即用和热插拔功能 ...

  10. 面向连接的Socket Server的简单实现(简明易懂)

    一.基本原理 有时候我们需要实现一个公共的模块,需要对多个其他的模块提供服务,最常用的方式就是实现一个Socket Server,接受客户的请求,并返回给客户结果. 这经常涉及到如果管理多个连接及如何 ...