一 原题
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
二 分析
三 代码
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!
/*
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;
}

本文介绍了一个基于Dijkstra算法的应用案例,该案例通过使用C++编程解决了一个关于寻找从多个起点到单一终点最短路径的问题。文章详细展示了如何初始化图结构、应用Dijkstra算法并最终确定到达目的地最快的一头奶牛。
812

被折叠的 条评论
为什么被折叠?



