usaco题目分享——Bessie Come Home

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了usaco题目分享——Bessie Come Home相关的知识,希望对你有一定的参考价值。

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
  这道题目是十分经典的最短路题目,要注意的就是A,a,Z等的处理。
  建图,然后spfa,dijikstra......都可以,这道题目没有卡。裸题可练手。
  
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<deque>
using namespace std;
int n;
vector <int> f[10005],w[10005];
int ne[10005],o,oo;
map<char,int> u;
char name[10005],ansname;
int zhong,ans=2000000000;
int inf=2000000000;
int dist[10005],in[10005];
void spfa(int z)
{
    int i,j;
    deque<int> dp;
    for(i=1;i<=oo;++i)
    {
        dist[i]=inf;
        in[i]=0;
    }
    in[z]=1;dist[z]=0;
    dp.push_front(z);
    while(!dp.empty())
    {
        i=dp.front();
        dp.pop_front();
        in[i]=0;
        for(j=0;j<f[i].size();++j)
        {
            int to=f[i][j];
            if(dist[i]<inf&&dist[to]>dist[i]+w[i][j])
            {
                dist[to]=dist[i]+w[i][j];
                if(!in[to])
                {
                    in[to]=1;
                    if(!dp.empty())
                    {
                        if(dist[to]<dist[dp.front()]) dp.push_front(to);
                        else dp.push_back(to);
                    }
                    else dp.push_back(to);
                }
            }
        }
    }
    if(dist[zhong]<ans)
    {
        ans=dist[zhong];
        ansname=name[z];
    }
}
int main()
{
    freopen("comehome.in","r",stdin);
    freopen("comehome.out","w",stdout);
    int i,j;
    scanf("%d",&n);
    char du;
    scanf("%c",&du);
    char a,b;
    int c;
    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题目分享——Bessie Come Home的主要内容,如果未能解决你的问题,请参考以下文章

洛谷P1529 回家 Bessie Come Home

洛谷——P1529 回家 Bessie Come Home

洛谷 P1529 回家 Bessie Come Home

luogu P1529 回家 Bessie Come Home

洛谷 P1529 回家 Bessie Come Home Label:Dijkstra最短路 && 乱搞

Bessie Come Home 回家(最短路)