题干:

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

题目大意:

    成语接龙游戏,多组输入,输入格式为:   第一行是成语个数n,后面n行:“当前成语出发找下一个成语” 的权值(题目描述为 时间),以及后面的汉字(以16进制表示,最少三个汉字),首尾相连找从1连到n的最短路。

解题报告:

     成语接龙游戏,权值val(不是这个题的maze啊)比较有意思,跟这题的权值maze有点类似:【POJ - 3037】Skiing (Dijkstra算法),相似之处在于权值只与边的起点有关  即:同一个Point发出的边的边权是相同的。poj3037那个题就是利用了这一点,推出了起点(1,1)和任一点的maze值。而此题是通过val来更新的maze值。思想类似。这题亦可以用map来保存字符串的首尾字符。

AC代码:

#include<bits/stdc++.h>

using namespace std;
const int MAX = 1000 + 5;
const int INF = 0x3f3f3f3f;
int len[MAX],val[MAX];
char str[MAX][MAX];
//
bool vis[MAX];
int maze[MAX][MAX],dis[MAX];
int n;
void Dijkstra(int u,int v) {
	int minv,minw,all = n;
	dis[u] = 0;
	while(all--) {
		minw = INF;
		for(int i = 1; i<=n; i++) {
			if(!vis[i] && dis[i]<minw) {
				minv = i;
				minw = dis[i];
			}
		}
		vis[minv] = 1;
		if(minv == v) return ;
		for(int i = 1; i<=n; i++) {
			if(!vis[i] && dis[i]>dis[minv] + ma***v][i]) dis[i] = dis[minv] + ma***v][i];
		}
	}
	
}


void init() {
	memset(len,0,sizeof(len) );
	memset(val,INF,sizeof(val) );
	memset(vis,0,sizeof(vis));
	memset(maze,INF,sizeof(maze));
	memset(dis,INF,sizeof(dis));
}
bool ok(int i, int j) {
	if(str[i][len[i]-1] == str[j][3] && str[i][len[i] - 2] == str[j][2] && str[i][len[i]-3] == str[j][1] && str[i][len[i]-4] == str[j][0]) {
		return true;
	}
	return false;
}
int main()
{
	while(~scanf("%d",&n) ) {
		if(n == 0) break;
		init();
		for(int i = 1; i<=n; i++) {
			scanf("%d %s",&val[i],str[i]);
			len[i] = strlen(str[i]);
		}
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=n; j++) {
				if(ok(i,j)) maze[i][j] = val[i];
			}
		}
//		for(int i = 1; i<=n; i++) {
//			for(int j = 1; j<=n; j++) {
//				printf("%d ",maze[i][j]) ;
//			}
//			printf("\n");
//		}
		Dijkstra(1,n);
		if(dis[n] == INF) printf("-1\n");
		else printf("%d\n",dis[n]);
	}
	return 0 ;
 } 

总结:

    1.还是那句话 写好了 init()函数不调用是什么操作?

    2.init中对数组的初始化别落下,,比如这次就落下了dis数组。