poj 3159 dijkstra 最短路
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 最短路的更多相关文章
- Candies POJ - 3159 (最短路+差分约束)
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 1161 Walls(最短路+枚举)
POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...
- Dijkstra最短路算法
Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...
- dijkstra(最短路)和Prim(最小生成树)下的堆优化
dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...
- 【坐在马桶上看算法】算法7:Dijkstra最短路算法
上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径 ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
随机推荐
- flash背景透明兼容ie火狐
<embed src="1234.swf" quality="high" type="application/x-shockwave-flash ...
- Python的排序
1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I' ...
- WP8 学习 在APP.XAML中加入Resources
<Application.Resources> <local:LocalizedStrings xmlns:local="clr-namespace:test1" ...
- Hibernate中的组合映射
1.实体bean设计 car: public class Car { private int id; private String name; private Wheel wheel; set... ...
- 在VS中使用类模板出现出现LNK2019: 无法解析的外部符号错误。
在VS中使用类模板出现出现LNK2019: 无法解析的外部符号错误,应在一个.h文件中完成方法的声明与实现,不要将实现放在cpp文件里,VS貌似不支持类模板分离
- python解无忧公主数学题108
""" python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...
- SpringMvc 单例
struts2的controller是多例,是因为其中有modeldriven将比如user 或者其他属性暴露出来,接受属性,特别是继承了actionsupport之后,fielderror的属性也会 ...
- pyqt5 笔记(三)py2exe 实现代码打包exe
python3.4 安装64位的版本 py2exe 下载地址: https://pypi.python.org/pypi/py2exe/0.9.2.0#downloads cmd——>进入pyf ...
- self.view 的不当操作造成死循环
如题,在创建ContentView的时候,例子如下 NSString *viewClassName = NSStringFromClass([self class]); viewClassName = ...
- hdu 2056
ps: - -惭愧...是套用一个大神的计算方法来做的.....下面是代码 代码: #include "stdio.h"#include "stdlib.h&quo ...