【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)
題干:
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 0Sample 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] + maze[minv][i]) dis[i] = dis[minv] + maze[minv][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數組。
總結
以上是生活随笔為你收集整理的【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国恒大突然被申请清盘!有何影响?
- 下一篇: 安卓最年轻手机玩家降临 Nothing