USACO Section 2.4 Bessie Come Home(dijkstra)

Bessie Come Home
Kolstad & Burch

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

Between milkings, each cow is located in her own pasture, though some pastures have no cows in them. Each pasture is connected by a path to one or more other pastures (potentially including itself). Sometimes, two (potentially self-same) pastures are connected by more than one path. One or more of the pastures has a path to the barn. Thus, all cows have 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 are 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 are in the barn, though.

PROGRAM NAME: comehome

INPUT 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, two 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 6
B d 3
C e 9
d Z 8
e Z 3

OUTPUT FORMAT

A single line containing two 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

题意:一群牛要回去吃饭晚餐,求最快到家的,输入中只有大小写字母,只有大写字母中有牛,大写字母Z为终点。
分析:以 Z 为起点的dijkstra
View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: comehome
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define MAXN 100
#define INF 0x3fffffff

using namespace std;

int map[MAXN][MAXN],lowcost[MAXN],d[MAXN];
bool visited[MAXN];

void dijkstra(int s,int n)
{
    int i,j,k,Min;
    memset(visited,false,sizeof(visited));
    for(i=0;i<n;i++)
    {
        lowcost[i]=map[s][i];
        d[i]=INF;
    }
    d[s]=0;
    visited[s]=true;
    for(i=0;i<n;i++)
    {
        Min=INF;k='Z'-'A';
        for(j=0;j<n;j++)
        {
            if(!visited[j]&&Min>lowcost[j])
            {
                Min=lowcost[j];
                k=j;
            }
        }
        d[k]=Min;
        visited[k]=true;
        for(j=0;j<n;j++)
        {
            if(!visited[j]&&lowcost[j]>lowcost[k]+map[k][j])
                lowcost[j]=lowcost[k]+map[k][j];
        }
    }
}

int main()
{
    freopen("comehome.in","r",stdin);
    freopen("comehome.out","w",stdout);
    int n,w,i,j;
    char u,v,N;
    while(scanf("%d",&n)==1)
    {
        for(i=0;i<='z'-'A';i++)
            for(j=0;j<='z'-'A';j++)
                map[i][j]=(i==j?0:INF);
        N='A';
        getchar();
        for(i=0;i<n;i++)
        {
            scanf("%c %c %d",&u,&v,&w);
            getchar();
            N=max(N,u);
            N=max(N,v);
            if(map[v-'A'][u-'A']>w)
                map[v-'A'][u-'A']=map[u-'A'][v-'A']=w;
        }
        dijkstra('Z'-'A',N-'A'+1);
        int ans_p,ans_min;
        ans_min=INF;
        for(i=0;i<'Z'-'A';i++)
            if(ans_min>d[i])
                ans_min=d[i],ans_p=i;
        printf("%c %d\n",ans_p+'A',ans_min);
    }
    return 0;
}
posted @ 2012-09-07 16:59  mtry  阅读(389)  评论(0编辑  收藏  举报