usaco2.4.4 Bessie Come Home
2016-12-19 15:59
274 查看
一 原题
Bessie Come HomeKolstad & 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
二 分析
dijkstra求单源最短路。因为INF开小WA了2次TAT
三 代码
运行结果:USER: Qi Shen [maxkibb3] TASK: comehome LANG: C++ Compiling... Compile: OK Executing... 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 your
submission #3 for this problem. Congratulations!
AC代码:
/* ID:maxkibb3 LANG: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; }
相关文章推荐
- 基于Java实现的Dijkstra算法示例
- Dijkstra和floyd——求单源点最短路径
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- Dijkstra算法的粗略学习
- USACO 3.2.2:Stringsobits
- usaco 1.1
- Milking Cows
- Transformations
- 【日常练习 dijkstra】POJ 2387 Til the Cows Come Home
- 【高手回避】poj3268,一道很水的dijkstra算法题
- poj2387 Til the Cows Come Home—Dijkstra模板
- YEN算法和删除算法分别实现K可靠最短路径算法
- Dijkstra 算法实现及问题
- USACO 3.1 Shaping Regions
- Djkstra
- 最短路
- 一个人的旅行
- HDU 2544
- pat甲级_路径问题(例题:pat 1003 Emergency (25))