Simple POJ 2387 Til The cows Come Home,dijkstra.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.