24h購物| | PChome| 登入
2013-07-31 16:44:35| 人氣2,089| 回應0 | 上一篇 | 下一篇

[UVA] 1247 - Interstar Transport

推薦 0 收藏 0 轉貼0 訂閱站台

By 2100, space travel will be a reality for the Milky Way (Solar Galaxy) residents. The Interstar Transport Travel agency operates scheduled direct space transports (flights) between some of the most popular planet destinations in the Milky Way. The cost of these scheduled direct transports are predetermined in Galaro (Galaxy Currency unit) and are published in many different languages. For travel to planets that is not on the schedule, there are slower, yet free, space flights from the closest planet that is on the direct transport list. To help travelers plan their itinerary, the Interstar Transport wants to offer a mobile application that can find the best traveling option, based on the total cost of the direct transports on the itinerary. Given the starting and destination planets on the itinerary, find the sequence of direct transports that has the lowest total traveling cost. Output all the planets in sequence that one must pass through on this best route. If two or more routes exist with the same cost, then the route that goes through the least number of intermediate planets is considered a better route. There will always exist a unique best route for any of the given test cases.


Technical Specification

  1. The number of planets on the direct transport list is at most s, 1$ le$s$ le$26. The planets are labeled using capital letters of the English alphabets, i.e., ``A", ``B", ``C", ..., ``Z", in no particular order.
  2. The Interstar Transport operates at most p, 1$ le$p$ le$200, direct transports between planets. There is at most one (could be none) direct transport between any two distinct planets.
  3. The cost of any transport is given as a natural number less than or equal to 100 Galaros.

Input 

The first line of the input file contains two integers, s and p, separated by a space. The next p lines each contains two letters, ei and ej, followed by a natural number, dij, indicating that there exists a direct transport between planets ei and ej with a cost of dij. The next line contains an integer n$ le$20, indicating the number of queries to follow. For each of the next n lines, each line contains two letters ek and em, indicating a user is looking for a best (lowest cost) way to get from planet ek to planet em.

Output 

For each of the n queries in the input, output on one line the best route to take, in the sequence of starting planet, the intermediate planets in sequence along the route and the destination planet; all separated by one blank space.

Sample Input 

5 7 
A B 1
B C 2
C D 3
D E 2
E A 1
A D 3
A C 4
3 
B D 
A D 
E C

Sample Output 

B A D 
A D 
E A B C

單純地找最短路徑後打印出結果。


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
int g[30][30] = {};
void spfa(int st, int ed) {
int dist[30][30], pre[30][30];
int inq[30][30] = {};
int i, j;
for(i = 0; i < 26; i++)
for(j = 0; j < 26; j++)
dist[i][j] = 0xfffffff;
dist[st][0] = 0;
queue<int> X, P;
X.push(st), P.push(0);
while(!X.empty()) {
int x = X.front();
int p = P.front();
X.pop(), P.pop();
inq[x][p] = 0;
for(i = 0; i < 26; i++) {
if(dist[i][p+1] > dist[x][p] + g[x][i]) {
dist[i][p+1] = dist[x][p] + g[x][i];
pre[i][p+1] = x;
if(inq[i][p+1] == 0) {
inq[i][p+1] = 1;
X.push(i), P.push(p+1);
}
}
}
}
int mn = dist[ed][0], x = ed, p = 0;
for(i = 0; i < 26; i++)
if(dist[ed][i] < mn)
mn = dist[ed][i], p = i;
printf("%c", x+'A');
while(x != st) {
x = pre[x][p];
p--;
// printf("%d\n", p);
printf(" %c", x+'A');
}
puts("");
}
int main() {
int n, m, q;
int i, j, k;
while(scanf("%d %d", &n, &m) == 2) {
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++)
g[i][j] = 0xfffffff;
g[i][i] = 0;
}
char s1[5], s2[5];
int v;
while(m--) {
scanf("%s %s %d", s1, s2, &v);
g[s1[0]-'A'][s2[0]-'A'] = v;
g[s2[0]-'A'][s1[0]-'A'] = v;
}
scanf("%d", &q);
while(q--) {
scanf("%s %s", s1, s2);
spfa(s2[0]-'A', s1[0]-'A');
}
}
return 0;
}
 

台長: Morris
人氣(2,089) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 12319 - Edgetown's Traffic Jams
此分類上一篇:[UVA] 10724 - Road Construction

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文