Idiomatic Phrases Game
Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two given idioms. For every two adjacent idioms, the last Chinese character of the former idiom should be the same as the first character of the latter one. For each time, Tom has a dictionary that he must pick idioms from and each idiom in the dictionary has a value indicates how long Tom will take to find the next proper idiom in the final list. Now you are asked to write a program to compute the shortest time Tom will take by giving you the idiom dictionary.
Input
The input consists of several test cases. Each test case contains an idiom dictionary. The dictionary is started by an integer N (0 < N < 1000) in one line. The following is N lines. Each line contains an integer T (the time Tom will take to work out) and an idiom. One idiom consists of several Chinese characters (at least 3) and one Chinese character consists of four hex digit (i.e., 0 to 9 and A to F). Note that the first and last idioms in the dictionary are the source and target idioms in the game. The input ends up with a case that N = 0. Do not process this case.
Output
One line for each case. Output an integer indicating the shortest time Tome will take. If the list can not be built, please output -1.
Sample Input
5
5 12345978ABCD2341
5 23415608ACBD3412
7 34125678AEFD4123
15 23415673ACC34123
4 41235673FBCD2156
2
20 12345678ABCD
30 DCBF5432167D
0
Sample Output
17
-1
题目类型:单源最短路
算法分析:读入短语并分析得出前4位和后4位的值from和to,然后按照得到的是from和to来建立edge邻接阵,最后调用dijkstra算法判断最后一个节点的最短路径dis[n-1]情况即可。注意对于有向图的邻接阵来说,不能只考虑矩阵上三角中元素的情况!!!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 1000 + 66; const int INF = 66666666; struct Node { int w; char from[8], to[8]; }; Node dict[maxn]; int edge[maxn][maxn]; int dis[maxn]; bool is_visited[maxn]; int n; char input_val[666]; void Dijkstra () { int i; for (i = 0; i < n; i++) dis[i] = edge[0][i]; memset (is_visited, false, sizeof (is_visited)); is_visited[0] = true; dis[0] = 0; for (i = 1; i < n; i++) { int j, minval = INF, index = 0; for (j = 1; j < n; j++) { if (!is_visited[j] && dis[j] < minval) { minval = dis[j]; index = j; } } is_visited[index] = true; for (j = 1; j < n; j++) { if (!is_visited[j] && edge[index][j] < INF && dis[j] > dis[index] + edge[index][j]) { dis[j] = dis[index] + edge[index][j]; } } } if (dis[n-1] == INF) cout << "-1" << endl; else cout << dis[n-1] << endl; } int main() { // ifstream cin ("aaa.txt"); while (cin >> n) { if (n == 0) break; int i; int input_w; for (i = 0; i < n; i++) { cin >> input_w >> input_val; int j, k, len = strlen (input_val); for (j = 0, k = len - 4; j < 4; j++, k++) { dict[i].from[j] = input_val[j]; dict[i].to[j] = input_val[k]; } dict[i].from[4] = dict[i].to[4] = 0; dict[i].w = input_w; } int j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { edge[i][j] = INF; if (i == j) continue; if (!strcmp (dict[i].to, dict[j].from)) edge[i][j] = dict[i].w; } } Dijkstra (); } return 0; } |
- « 上一篇:zoj2412
- zoj2797:下一篇 »