poj 3159 dijkstra 最短路
2015-07-28 00:05
411 查看
Description
Duringthekindergartendays,flymousewasthemonitorofhisclass.Occasionallythehead-teacherbroughtthekidsofflymouse’sclassalargebagofcandiesandhadflymousedistributethem.Allthekidslovedcandiesverymuchandoftencomparedthenumbersofcandiestheygotwithothers.AkidAcouldhadtheideathatthoughitmightbethecasethatanotherkidBwasbetterthanhiminsomeaspectandthereforehadareasonfordeservingmorecandiesthanhedid,heshouldnevergetacertainnumberofcandiesfewerthanBdidnomatterhowmanycandiesheactuallygot,otherwisehewouldfeeldissatisfiedandgotothehead-teachertocomplainaboutflymouse’sbiaseddistribution.
snoopysharedclasswithflymouseatthattime.flymousealwayscomparedthenumberofhiscandieswiththatofsnoopy’s.Hewantedtomakethedifferencebetweenthenumbersaslargeaspossiblewhilekeepingeverykidsatisfied.Nowhehadjustgotanotherbagofcandiesfromthehead-teacher,whatwasthelargestdifferencehecouldmakeoutofit?
Input
Theinputcontainsasingletestcases.ThetestcasesstartswithalinewithtwointegersNandMnotexceeding30000and150000respectively.Nisthenumberofkidsintheclassandthekidswerenumbered1throughN.snoopyandflymousewerealwaysnumbered1andN.ThenfollowMlineseachholdingthreeintegersA,Bandcinorder,meaningthatkidAbelievedthatkidBshouldnevergetoverccandiesmorethanhedid.
Output
Outputonelinewithonlythelargestdifferencedesired.Thedifferenceisguaranteedtobefinite.
SampleInput
SampleOutput
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
usingnamespacestd;
structnode
{
inte;
intw;
boolfriendoperator<(nodes1,nodes2)
{
returns1.w>s2.w;
}
};
vector<vector<node>>edge;
priority_queue<node>q;
boolbeused[30005];
nodeww;
intmain()
{
intn,m;
intA,B,C;
scanf("%d%d",&n,&m);
edge.clear();
edge.resize(n+1);
for(inti=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;
intj=edge[ww.e].size();
for(inti=0;i<j;i++)
{
noder;
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);
return0;
}
Duringthekindergartendays,flymousewasthemonitorofhisclass.Occasionallythehead-teacherbroughtthekidsofflymouse’sclassalargebagofcandiesandhadflymousedistributethem.Allthekidslovedcandiesverymuchandoftencomparedthenumbersofcandiestheygotwithothers.AkidAcouldhadtheideathatthoughitmightbethecasethatanotherkidBwasbetterthanhiminsomeaspectandthereforehadareasonfordeservingmorecandiesthanhedid,heshouldnevergetacertainnumberofcandiesfewerthanBdidnomatterhowmanycandiesheactuallygot,otherwisehewouldfeeldissatisfiedandgotothehead-teachertocomplainaboutflymouse’sbiaseddistribution.
snoopysharedclasswithflymouseatthattime.flymousealwayscomparedthenumberofhiscandieswiththatofsnoopy’s.Hewantedtomakethedifferencebetweenthenumbersaslargeaspossiblewhilekeepingeverykidsatisfied.Nowhehadjustgotanotherbagofcandiesfromthehead-teacher,whatwasthelargestdifferencehecouldmakeoutofit?
Input
Theinputcontainsasingletestcases.ThetestcasesstartswithalinewithtwointegersNandMnotexceeding30000and150000respectively.Nisthenumberofkidsintheclassandthekidswerenumbered1throughN.snoopyandflymousewerealwaysnumbered1andN.ThenfollowMlineseachholdingthreeintegersA,Bandcinorder,meaningthatkidAbelievedthatkidBshouldnevergetoverccandiesmorethanhedid.
Output
Outputonelinewithonlythelargestdifferencedesired.Thedifferenceisguaranteedtobefinite.
SampleInput
22 125 214
SampleOutput
5 分析题意:属于单源最短路 dijkstra+优先队列代码如下
相关文章推荐
- 黑马程序员——自学总结(六)反射
- 成员变量的作用域
- Java通过Json传递数据给JavaScript
- 源码阅读——十个C开源项目
- HDU 4283 You Are the One(区间dp)
- Linux 运维工程师入门须掌握的 10 个技术点
- 查找二叉树(即二叉排序树)的学习
- 讨论CSS中的各类居中方式
- JavaScript类型系统之正则表达式
- 常用Linux Shell进阶部分小结
- 设计windows phone页面主题
- 分享PHP函数实现数字与文字分页代码
- 怎样搭建PHP开发环境
- 图文介绍PHP添加Redis模块及连接
- 如何实现php图片等比例缩放
- python追加元素到列表的方法
- python比较两个列表是否相等的方法
- python通过加号运算符操作列表的方法
- python实现给数组按片赋值的方法
- python更新列表的方法