USACO 2.4 D Bessie Come Home 题解
来源:互联网 发布:h5制作软件下载 编辑:程序博客网 时间:2024/05/22 01:28
2.4.D Bessie Come Home
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
#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <iostream>using namespace std;int P, w;int G[52][52], dist[52];char u, v, ans;bool visited[52];char Code(char ch){return isupper(ch)?ch-65:ch-71;}void Dijkstra(int v){ memcpy(dist, G[v], sizeof(dist)); visited[v] = true; for(int i = 1; i<52; ++i){ int best = 0x7FFFFFFF, best_j = -1; for(int j = 0; j<52; ++j)if(!visited[j] && dist[j]<best) best = dist[j], best_j = j; if(best_j<0) break; if(best_j<25){ ans = char(65+best_j); w = best; return; } visited[best_j] = true; for(int j = 0; j<52; ++j)if(!visited[j] && dist[j]>best+G[best_j][j]) dist[j] = best+G[best_j][j]; }}int main(){ freopen("comehome.in", "r", stdin); freopen("comehome.out", "w", stdout); memset(G, 0x7F, sizeof(G)); scanf("%d\n", &P); for(int i = 0; i<P; ++i){ scanf("%c %c %d\n", &u, &v, &w); u = Code(u); v = Code(v); if(G[u][v]>w) G[u][v] = G[v][u] = w; } Dijkstra(25); printf("%c %d\n", ans, w); fclose(stdin); fclose(stdout); return 0;}
- USACO 2.4 D Bessie Come Home 题解
- USACO--2.4Bessie Come Home
- usaco 2.4 bessie come home
- [usaco]Bessie Come Home
- usaco Bessie Come Home
- USACO:Bessie Come Home
- USACO-Bessie Come Home
- USACO Bessie Come Home
- USACO 2.4.4 Bessie Come Home
- USACO 2.4 Bessie Come Home (comehome)
- usaco 2.4 PROB Bessie Come Home<水>
- usaco 2.4.4 Bessie Come Home
- USACO section2.4 Bessie Come Home题解&代码
- 【图论】【USACO】Bessie Come Home
- USACO section 2.4 Bessie Come Home(最短路)
- USACO 2.4 Bessie Come Home (Floyd最短路径)
- USACO-Section 2.4 Bessie Come Home(最短路[Dijkstra])
- 【USACO题库】2.4.4 Bessie Come Home回家
- java 在线编辑模版 代码编辑器 兼容手机平板PC freemaker 静态引擎 网站源码
- 下载和安装python
- #python练习#leetcode392. Is Subsequence【贪心】
- 欢迎使用CSDN-markdown编辑器
- Android 签名时 v2 与 v1 的选择
- USACO 2.4 D Bessie Come Home 题解
- LibreOJ504
- 对话框报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.
- 安卓复杂滑动案例 自定义behavior源码分析 实现头布局图片的缩放透明度变化,RecycleView的滑动布局,坐标变化
- nginx、php-fpm等添加自启动
- 解决批量解密DES文件(以图片为例子)
- RequireJS
- 北大大数据机试题
- 有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<