Description
Bessie is out of field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her For the morning milking. Bessie needs she beauty sleep, so she wants to get back as quickly as possible.
Farmer John ' s field has N (2 <= n <=) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; The Apple Tree Grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) Bidirectional cow-trails of various lengths between the landmarks. Bessie isn't confident of her navigation ability, so she's always stays on a trail from its start to its end once she start S it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It's guaranteed that some such route exists. Shortest-circuit template problem ... The code is as follows:
#include <iostream>#include<cstring>#include<queue>using namespacestd;/////////////////////////////////////////////////////////////////Const intmaxn=1010;Const intinf=10e8;structnode{intV,val; Node (int_v=0,int_val=0): V (_v), Val (_val) {}BOOL operator< (ConstNode &a)Const { returnVal>A.val; }};structedge{intV,cost; Edge (int_v=0,int_cost=0): V (_v), cost (_cost) {}};vector<Edge>E[MAXN];BOOLVIS[MAXN];voidDijkstra (intLowcost[],intNintstart) {Priority_queue<Node>que; Node qtemp; intLen; intU,v,cost; for(intI=1; i<=n;++i) {Lowcost[i]=INF; Vis[i]=0; } Lowcost[start]=0; Que.push (Node (Start,0)); while(!Que.empty ()) {Qtemp=Que.top (); Que.pop (); U=qtemp.v; if(Vis[u])Continue; Vis[u]=1; Len=e[u].size (); for(intI=0; i<len;++i) {v=e[u][i].v; Cost=E[u][i].cost; if(!vis[v] && lowcost[v]>lowcost[u]+Cost ) {Lowcost[v]=lowcost[u]+Cost ; Que.push (Node (v,lowcost[v)); }}}}inlinevoidAddedge (intUintVintc) {E[u].push_back (Edge (V,c));}/////////////////////////////////////////////////////////////////intans[1010];intMain () {Ios::sync_with_stdio (false); intn,t; intA,b,c; CIN>>T>>N; for(intI=1; i<=t;++i) {cin>>a>>b>>C; Addedge (A,B,C); Addedge (B,A,C); } Dijkstra (Ans,n,n); cout<<ans[1]<<Endl; return 0; }
View Code
Simple POJ 2387 Til The cows Come Home,dijkstra.