Usaco topic sharing--bessie Come Home

Source: Internet
Author: User

Bessie Come Home
Kolstad & Burch

It ' s dinner time, and the cows is out in their separate pastures. Farmer John rings The bell so they would start walking to the barn. Your job is-to-figure out which one cow gets to the barn first (the supplied test data would always have exactly one fastes T cow).

Between milkings, each cow was located in her own pasture, though some pastures has no cows in them. Each pasture are connected by a path to one or more pastures (potentially including itself). Sometimes, potentially self-same pastures is connected by more than one path. One or more of the pastures have a path to the barn. Thus, all cows has a path to the barn and they always know the shortest path. Of course, cows can go either direction on a path and they all walk at the same speed.

The pastures is labeled ' a '. ' Z ' and ' A '. ' Y '. One cow is in each pasture labeled with a capital letter. No Cow is in a pasture labeled with a lower case letter. The Barn ' s label is ' Z '; No cows is in the barn, though.

Program Name:comehomeinput FORMAT
Line 1: Integer p (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn)
Line 2..p+1: Space separated, letters and an integer:the names of the interconnected pastures/barn and the distance between them ( 1 <= Distance <= 1000)
SAMPLE INPUT (file comehome.in)
5 a D 6B d 3C e 9d z 8e z 3
OUTPUT FORMAT

A single line containing-items:the capital letter name of the pasture of the cow that arrives first back at the barn, The length of the path followed by that cow.

SAMPLE OUTPUT (file comehome.out)
B 11
This topic is a very classic short-circuit problem, to pay attention to is the treatment of a,a,z and so on. Build, then Spfa,dijikstra ... Yes, this problem has no card. Bare questions can be practiced hand.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<deque>using namespacestd;intN;vector<int> f[10005],w[10005];intMex10005],o,oo;map<Char,int>u;Charname[10005],ansname;intzhong,ans=2000000000;intinf=2000000000;intdist[10005],inch[10005];voidSPFA (intz) {    inti,j; Deque<int>DP;  for(i=1; i<=oo;++i) {Dist[i]=inf; inch[i]=0; }    inch[z]=1;d ist[z]=0;    Dp.push_front (z);  while(!Dp.empty ()) {i=Dp.front ();        Dp.pop_front (); inch[i]=0;  for(j=0; J<f[i].size (); + +j) {intto=F[i][j]; if(dist[i]<inf&&dist[to]>dist[i]+W[i][j]) {Dist[to]=dist[i]+W[i][j]; if(!inch[to]) {                    inch[to]=1; if(!Dp.empty ()) {                        if(dist[to]<Dist[dp.front ()]) Dp.push_front (to); ElseDp.push_back (to); }                    ElseDp.push_back (to); }            }        }    }    if(dist[zhong]<ans) {ans=Dist[zhong]; Ansname=Name[z]; }}intMain () {Freopen ("comehome.in","R", stdin); Freopen ("Comehome.out","W", stdout); inti,j; scanf ("%d",&N); Chardu; scanf ("%c",&du); Charb; intC;  for(i=1; i<=n;++i) {scanf ("%c%c%d",&a,&b,&c); scanf ("%c",&du); if(u[a]==0) {oo++; U[a]=Oo; Name[oo]=A; }        if(u[b]==0) {oo++; U[B]=Oo; Name[oo]=b;        } f[u[a]].push_back (U[b]);        W[u[a]].push_back (c);        F[u[b]].push_back (U[a]);        W[u[b]].push_back (c); if(a=='Z') zhong=U[a]; if(a>='A'&&a<='Y') {o++; Ne[o]=U[a]; }        if(b=='Z') zhong=U[b]; if(b>='A'&&b<='Y') {o++; Ne[o]=U[b]; }    }     for(i=1; i<=o;++i) {SPFA (ne[i]); } printf ("%c%d\n", Ansname,ans); return 0;}

Usaco topic sharing--bessie Come Home

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.