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;
}
时间: 2024-08-09 10:44:37

usaco题目分享——Bessie Come Home的相关文章

usaco题目分享——Longest Prefix

Longest PrefixIOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denoted by an uppercase letter. Biologists are interested in decomposing a long sequence into shorter sequences cal

*usaco题目分享——Contact

ContactIOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right from the centre of the galaxy. They wish to know if the

usaco题目分享——Controlling Companies

Controlling Companies Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford owns 12% of Mazda. It is said that a company A controls company B if at least one of the foll

usaco题目分享——Agri-Net

Agri-NetRuss Cox Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is

模拟考试题目分享

1.多米诺骨牌(domino.pas) Jzabc对多米诺骨牌有很大的兴趣,然而他的骨牌比较特别,只有黑色的和白色的两种.他觉得如果存在连续三个骨牌是同一种颜色,那么这个骨牌排列便不是美观的.现在他有n个骨牌要来排列,他想知道不美观的排列个数.由于数字较大,数学不好的他又不会统计,所以他请你来帮忙.希望你在一秒内求出不美观的排列个数. [输入] 只有一个正整数,即要排列的骨牌个数. [输出] 一个数,即不美观的排列个数. [样例输入] 4 [样例输出] 6 [样例解释] 有四种不美观的排列. 黑

挖财 /有赞电话面试 题目分享

动画相关 //html <div class="btn"> <div class="change"></div> </div> //css .btn { width: 100px; height: 20px; background: coral; border: 1px solid #5e6d82; } .change { width: 0; height: 20px; background: #2D93CA; tra

金朝阳——软件测试试题11道题目分享

测试人员相对于开发人员来说,对知识的广度要求更高. 1:下面所描述的属于安全漏洞方面的有哪些?()A.SQL注入问题B.跨站脚本(XSS)C.不安全的加密存储,比如CSDN网站的用户密码是明文密码D.网站访问缓慢 2:关于Loadrunner下列说法正确的是()A.web_reg_save_param最常用來做关联的函数B. 函数lr_save_string("我是一名软件测试工程师","tester")的含义是"把我是一名软件测试工程师字符串赋了test

金朝阳——软件測试试题11道题目分享

測试人员相对于开发者来说.对知识的广度要求更高. 1:以下所描写叙述的属于安全漏洞方面的有哪些?() A.SQL注入问题 B.跨站脚本(XSS) C.不安全的加密存储,比方CSDN站点的用户password是明文password D.站点訪问缓慢 2:关于Loadrunner下列说法正确的是() A.web_reg_save_param最经常使用來做关联的函数 B. 函数lr_save_string("我是一名软件測试project师","tester")的含义是&

金朝阳——软件测试试题3道题目分享

1.设计测试用例的方法有很多,而且每种方法都有不同的适用条件.下列是对几种方法的描述: 对于业务流清晰的系统可以利用______贯穿整个测试用例设计过程:在用例中综合使用各种测试方法,对于参数配置类的软件要用______选择较少的组合方式达到最佳效果:如果程序的功能说明中含有输入条件的组合情况,则一开始就可以选用______. 针对以上描述,能够按照正确的顺序填入上面空白处的选项是().(选择一项) A.等价类划分法.因果图法.状态图法 B.场景法.正交排列法.因果图法 C.状态图法.等价类划分