您的位置:首页 > 其它

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

22
125
214

SampleOutput

5

分析题意:属于单源最短路
dijkstra+优先队列代码如下


#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; }


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: