usaco2.4.4 Bessie Come Home
来源:互联网 发布:工控软件 编辑:程序博客网 时间:2024/06/05 23:01
一 原题
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)
5A d 6B d 3C e 9d Z 8e 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
二 分析
三 代码
USER: Qi Shen [maxkibb3]TASK: comehomeLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 4192 KB] Test 2: TEST OK [0.000 secs, 4192 KB] Test 3: TEST OK [0.000 secs, 4192 KB] Test 4: TEST OK [0.000 secs, 4192 KB] Test 5: TEST OK [0.000 secs, 4192 KB] Test 6: TEST OK [0.000 secs, 4192 KB] Test 7: TEST OK [0.000 secs, 4192 KB] Test 8: TEST OK [0.000 secs, 4192 KB] Test 9: TEST OK [0.000 secs, 4192 KB]All tests OK.Your program ('comehome') produced all correct answers! This is yoursubmission #3 for this problem. Congratulations!
/*ID:maxkibb3LANG:C++PROG:comehome*/#include<iostream>#include<fstream>using namespace std;const int MAXN = 52;const int INF = 100005;ifstream fin;ofstream fout;int n;int dis[MAXN][MAXN];int ans = MAXN * INF;char ans_id;int f(char c) { if(c >= 'A' && c <= 'Z') return c - 'A'; else return c - 'a' + 26;}void init() { for(int i = 0; i < MAXN; i++) { for(int j = 0; j < MAXN; j++) { if(i == j) dis[i][j] = 0; else dis[i][j] = INF; } } fin.open("comehome.in"); fout.open("comehome.out"); fin >> n; char s, e; int len; for(int i = 0; i < n; i++) { fin >> s >> e >> len; int _s = f(s), _e = f(e); if(len < dis[_s][_e]) dis[_s][_e] = dis[_e][_s] = len; }}int dijkstra(int s) { bool v[MAXN] = {false}; v[s] = true; int _dis[MAXN]; int ret = INF; for(int i = 0; i < MAXN; i++) _dis[i] = dis[s][i]; while(true) { int min_dis = INF, min_idx; for(int i = 0; i < MAXN; i++) { if(v[i]) continue; if(_dis[i] < min_dis) min_dis = _dis[i], min_idx = i; } if(min_dis == INF) break; if(min_idx == 25) { ret = min_dis; break; } v[min_idx] = true; for(int i = 0; i < MAXN; i++) { if(min_dis + dis[min_idx][i] < _dis[i]) _dis[i] = min_dis + dis[min_idx][i]; } } return ret;}int main() { init(); for(int i = 0; i < 25; i++) { int c = dijkstra(i); if(c < ans) { ans = c; ans_id = i + 'A'; } } fout << ans_id << " " << ans << endl; return 0;}
- usaco2.4.4 Bessie Come Home
- USACO2.4.4 Bessie Come Home (comehome)
- USACO2.4.4 Bessie Come Home (comehome)
- [USACO2.4]回家 Bessie Come Home
- [usaco]Bessie Come Home
- bessie come home
- usaco Bessie Come Home
- Bessie Come Home
- USACO:Bessie Come Home
- USACO-Bessie Come Home
- Bessie Come Home
- UVA Bessie Come Home
- USACO Bessie Come Home
- 回家 Bessie Come Home
- 【图论】【USACO】Bessie Come Home
- USACO--2.4Bessie Come Home
- usaco 2.4 bessie come home
- USACO:Bessie Come Home解题报告
- Java基础学习总结——super关键字
- 导出excel时需要在单元格进行强制换行2003与2007方法一样
- Spring bean
- angular2.0如何自定义管道?
- JavaScript中iframe获取其他frame中的元素值
- usaco2.4.4 Bessie Come Home
- HTTPS和HTTP的区别
- VS2013 编译XPdf
- 渗透测试教程(基础篇)-2
- 使用maven编译spark源码
- NP-Complete Stingy Set
- 读书笔记--自定义View --开发艺术探索
- Sublime Text 2搭建Java开发环境
- Windows自带截图工具