hdu 1546

Problem Description

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
#include <iostream>
#include <cstring>
#include <cstdio>
#define inf 1000000000
using namespace std;
int map[1005][1005];
char str[1005][1000];
int val[1005];
int vis[1005];
int cost[1005];
int n;
void dijkstra()
{
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i<n; i++)
		cost[i] = map[0][i];
	cost[0] = 0;
	vis[0] = 1;
	for (int i = 1; i<n; i++)
	{
		int  pos,min = inf;
		for (int j = 1; j<n; j++)
			if (!vis[j] && cost[j] <= min)
			{
				pos = j;
				min = cost[j];
			}
		vis[pos] = 1;
		for (int j = 1; j<n; j++)
		{
			if (!vis[j] && (cost[j]>cost[pos] + map[pos][j]))
			{
				cost[j] = cost[pos] + map[pos][j];
			}
		}
	}
}
int main()
{
	int len;
	while (scanf("%d", &n), n)
	{
		for (int i = 0; i<n; i++)
			scanf("%d%s", &val[i], str[i]);
		for (int i = 0; i<n; i++)
		{
			len = strlen(str[i]);
			for (int j = 1; j<n; j++)
			{
				if (i == j)
					continue;
				if (str[j][0] == str[i][len - 4] && str[j][1] == str[i][len - 3] && str[j][2] == str[i][len - 2] && str[j][3] == str[i][len - 1])
					map[i][j] = val[i];
				else
				{
					map[i][j] = inf;
				}
			}
		}
		dijkstra();
		if (n == 1)
			printf("-1\n");
		else
			printf("%d\n", cost[n - 1] == inf ? -1 : cost[n - 1]);
	}
	return 0;
}
				
时间: 2024-08-16 01:40:59

hdu 1546的相关文章

HDU 1546 Idiomatic Phrases Game

成语接龙. 上一个的尾必须和下一个的首相同.注意:花费的时间是上一个. 一开始我就建图建错了. 比如第 i 个成语 与第 j 个成语, 第  i 个成语前面的时间为 t : 建图为 i -> j = t: 基友说这<图论算法理论.实现及应用>上有一样的题,我借来看了一下,发现它建图似乎有错误. 0->3 这条边的权值似乎错了. 反正我的建图是这样的 0->1=5;0->3=5; 1->2=5; 2->4=7; 3->4=15; 求 0 -> 4

Idiomatic Phrases Game HDU 1546

Description 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

hdu 1546(dijkstra)

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3154    Accepted Submission(s): 1026 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

hdu 1546 Idiomatic Phrases Game 最短路

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2283    Accepted Submission(s): 742 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

HDU 1546 Idiomatic Phrases Game(dijkstra+优先队列)

Idiomatic Phrases Game Problem Description 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 li

hust 1546 &amp;&amp; hdu 3911 Black And White

题目描述 There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she want to know the l

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116