`
Simone_chou
  • 浏览: 195672 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Bessie Come Home(最短路 + Floyd)

 
阅读更多
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

 

     题意:

     给出 P(1 ~ 10000)条路,后给出这 P 条无向边的两端和距离(1 ~ 1000), 大写字母表示的是有牛的位置,小写字母表示该处没有牛但是有个转角点,终点是Z,求哪头牛最快到达终点,输出这头牛的编号和距离。

 

     思路:

     最短路 + Floyd。开 60 大小的数组,代表 A ~ Z 和 a ~ z 全部,同时用数组 cow 标记哪头牛是存在的,最后比较得出最小值即可。

 

     AC:

/*
TASK:comehome
LANG:C++
ID:sum-g1
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define INF 99999999
using namespace std;

int w[60][60];
bool cow[30];

void init() {
    for(int i = 0; i < 60; ++i)
            for(int j = 0; j < 60; ++j)
                    w[i][j] = INF;

    for(int i = 0; i <= 26; ++i)  cow[i] = false;
}

void floyd() {
    for(int k = 0; k < 60; ++k)
            for(int i = 0; i < 60; ++i)
                    for(int j = 0; j < 60; ++j)
                            if(w[i][k] < INF &&
                               w[k][j] < INF &&
                               w[i][j] > w[i][k] + w[k][j])
                               w[i][j] = w[i][k] + w[k][j];
}

int main() {
    freopen("comehome.in","r",stdin);
    freopen("comehome.out","w",stdout);
    int n;
    init();

    scanf("%d",&n);
    getchar();
    while(n--) {
        char a,b;
        int dis;
        scanf("%c %c%d",&a,&b,&dis);
        getchar();

        if(a >= 'A' && a <= 'Z') cow[a - 'A'] = true;
        if(b >= 'A' && b <= 'Z') cow[b - 'A'] = true;

        w[a - 'A'][b - 'A'] = min(w[a - 'A'][b - 'A'],dis);
        w[b - 'A'][a - 'A'] = min(w[b - 'A'][a - 'A'],dis);
    }

    floyd();

    int min_step = INF;
    char min_cow;

    for(int i = 0; i < 25; ++i)
            if(cow[i] && w[i][25] < min_step) {
                    min_step = w[i][25];
                    min_cow = 'A' + i;
            }

    printf("%c %d\n",min_cow,min_step);
    return 0;
}

 

 

分享到:
评论

相关推荐

    USACO-Bessie-Come-Home.zip_Home Home

    【标题】USACO-Bessie-Come-Home.zip_Home Home 【正文】 这个压缩包文件中的内容是关于USACO(美国计算机奥林匹克)竞赛中一个名为"Bessie Come Home"的问题的C++解决方案。USACO是一个针对高中生的在线编程竞赛...

    bessie_come_home.rar_Home Home

    《USACO编程挑战:“Bessie Come Home”——C++实现与BFS算法解析》 在编程竞赛的世界里,USACO(USA Computing Olympiad)是一个颇受欢迎的平台,它为参赛者提供了锻炼和提升算法技能的机会。本次我们要探讨的是...

    P2666 [USACO07OCT] Bessie's Secret Pasture S

    P2666 [USACO07OCT] Bessie's Secret Pasture S

    对冒泡排序的深入理解

    冒泡排序是一种基础的排序算法,它通过重复遍历待排序的序列,比较相邻元素并根据需要交换它们的位置,使得较大的元素逐渐“浮”到序列的末尾,就像水底下的气泡慢慢升到水面一样。这个过程会一直持续到序列变得有序...

    GEOS是一个C ++ 11库,用于在二维矢量几何上执行操作-C/C++开发

    GEOS是一个C ++ 11库,用于在二维矢量几何上执行操作。 它主要是JTS拓扑套件Java库的端口。...生成状态分支/ CI Debbie Winnie Dronie Travis CI GitLab CI AppVeyor Bessie Bessie32 master 3.8 3.7

    USACO-Cpp

    【USACO-Cpp】是针对USACO(美国计算机奥林匹克竞赛)的C++编程教程资源,主要面向参赛者提供C++语言的学习材料,帮助他们在比赛中解决算法和数据结构问题。USACO是一项旨在提升高中生计算机科学技能的比赛,强调...

    Mountain Watching

    Bessie在欣赏威斯康星州美丽的山脉时,她想知道哪座山是最宽的。为了找到答案,她使用了一种名为Acme Long Distance Geoaltimeter的设备,沿着地平线等距离地测量了N个高度值H_i。根据这些数据,我们需要找出所有...

    NOIP普及组模拟考试题目与标程

    奶牛Bessie最近在学习字符串操作,它用如下的规则逐一的构造出新的字符串:S(0)= “moo”;S(1)= S(0)+ “m”+ “ooo”+ S(0)= “moo”+ “m”+ “ooo”+ “moo”= “moomooomoo”;…。问题是:给出一个...

    Diamond-Collector(钻石收藏家):使用Phaser构建的平台游戏,玩家面临的挑战是在平台之间跳转,避开障碍物并收集钻石

    平台游戏JS Capstone 钻石收藏家 目录 现场演示 关于该项目 一个名为Diamond Collector的平台游戏,该游戏的想法取材于著名的Endless Runner游戏,但具有升级的UI,UX和动画。 建于 JavaScript ...

    Untitled3.cpp

    The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that ...

    KnapsackProblems(nocow版)1

    示例问题是一个农场主约翰为奶牛Bessie挑选音乐合辑的问题,目标是找到总长度不超过挤奶时间的歌曲组合,以最大化产奶量。这个问题可以抽象为背包问题,即在有限时间内选择最高价值的歌曲。 接着,文章详细讲解了...

    构建C 实例类Infact.zip

    Cow c1 = Cow(name("Bessie")); // Construct a second cow with a different name, and an optional age. // Also, specifying a type is optional, since InFact does type inference....

    Usaco2024 Open测试数据

    bronze-T1-Logical Moos bronze-T2-Walking Along a Fence bronze-T3-Farmer John's Favorite Permutation ...silver-T1-Bessie's Interview silver-T2-Painting Fence Posts silver-T3-The 'Winning' Gene

    PostGIS空间数据库扩展到PostgreSQL [mirror] - PostGIS / PostGIS

    Bessie: Bessie32: Berrie: Berrie64: This file is here to play nicely with modern code repository facilities. Actual readme is . Official code repository, issue tracker and wiki: Official chat room: ...

    SQL语法大全

    SQL语法大全 SQL语法大全 1. ASP与Access数据库连接: dim conn,mdbfile mdbfile=server.mappath("数据库名称.mdb") set conn=server.createobject("adodb.connection") conn.open "driver={microsoft access ...

    江苏省盐城市大丰区小海镇七年级英语上册满分冲刺第15讲阅读理解之猜词题讲义新版牛津版20180817160

    在题三中,“towering presence”与前面的“only about five feet tall”形成对比,暗示尽管Miss Bessie身材矮小,但她的影响力极大,答案是A,表明她对学生的深远影响。 4. **根据因果关系猜测词义**:题四中,...

    singsangsung

    成星成一个简单的基于计时器的游戏。 在仍有时间的情况下,帮助Bessie唱尽可能多的音符! 播放。口香糖机David Su的所有资产,Louis Fineberg的Life Is Goofy字体除外。

    天津市红桥区2021届高三英语下学期3月质量调查一模试题PDF2021041602122

    例如,56题可能要求学生概述某人的职业目标,57题可能询问某个决定背后的原因,58题可能是关于人生目标的比喻,59题要求评价人物的品质,而60题则是一个开放性问题,鼓励学生结合个人经历分享他们心中的“Bessie”,...

    eTOM中文

    eTOM,全称为Enhanced Telecom Operations Map,是电信行业的一种业务流程框架,旨在提高运营效率和服务质量。这个中文文档的出现,对于理解和应用eTOM框架在中国的电信企业中至关重要。eTOM是一个层次化的模型,...

Global site tag (gtag.js) - Google Analytics