题干:

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] + 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处理字符串)相关推荐

  1. HDU 1546 Idiomatic Phrases Game

    传送门 最短路. 这道题题意比较烦,其实就是成语接龙.给你一个成语的列表(字典)(每个成语用十六进制字符串表示),规定字典的第一个成语和最后一个成语是你完成成语接龙的起点和终点,然后你在字典里面选成语 ...

  2. HDU 1546 (最短路 Dijkstra算法)

    题目: Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese charac ...

  3. HDOJ 2112 HDU Today (最短路 Dijkstra SPFA)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. c++ map 修改value_干货 | 名企高频考点如何使用map统计字符串各个字符出现的次数...

    点击蓝字关注我哦 以下是本期干货视频视频后还附有文字版本哦▼<名企高频考点-如何使用map统计字符串各个字符出现的次数>▼ps:请在WiFi环境下打开,如果有钱任性请随意有某公司这样一道笔 ...

  5. HDU 1874 畅通工程续 (Dijkstra , Floyd , SPFA, Bellman_Ford 四种算法)

    畅通工程续 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修 ...

  6. HDU_2112 HDU Today—最短路(Dijkstra)

    点击这里 你便可以亲身去HDU体验一下 先看题目: HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  7. hdu 1546(最短路)

    题意:成语接龙的游戏,一个中文字是四个字符组成,所以只要一个字符串后四个字符与另一个字符串的头四个字符能匹配,那么就能接上,求出从第一个到最后一个的最短时间. 不知道是建图的问题还是什么,一直WA.. ...

  8. hdu 2112 HDU Today 最短路(Dijkstra算法)

    HDU Today                                                                 Time Limit: 15000/5000 MS ...

  9. HDU Today 【最短路径】+【构造map】

    HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

最新文章

  1. Linux exec与重定向
  2. 从assemblyer Instructure deepth understander C principle
  3. android 登录组件开发,Android组件化开发路由的设计
  4. CSS继承选择器与包含选择器的比较
  5. 学生系统优化(三)- -细节优化
  6. 下载:Visual Studio 2012 RC候选版
  7. DCMTK:简单存储服务类用户
  8. 【C/C++9】天气APP:Oracle的虚表/日期/序列,索引/视图/链路/同义词,数据库高可用性
  9. python基础知识点制作图片
  10. SQLite基本语法
  11. java 集成开发工具_最好的Java开发人员测试和集成工具
  12. 带有批注的Spring硒测试
  13. java判断斐波那契数列_Java 实例 - 斐波那契数列
  14. python中列表 字典 元祖 enumerate()函数
  15. dreamcast游戏_《Dreamcast Collection》开箱及游戏介绍
  16. python编程(多线程c回调python)
  17. git .gitignore file does not work
  18. FastReport.Net使用:[23]图表(Chart)控件
  19. jade选峰之后怎么去掉_jade使用教程
  20. excel实现分组计数

热门文章

  1. 操作系统进程调度先来先服务FCFS
  2. rs232串口驱动_电脑主板RS232串口硬件设计
  3. vue积累——另一种走马灯
  4. oracle jdbc jar包_Oracle总结之plsql编程(基础七)
  5. jsp java代码_jsp内的java代码不执行,急啊!!大家来看看!
  6. mysql主从进行扩展_Mysql主从知识扩展部分1
  7. 用VS2005开发WinCE程序调试图文教程
  8. 丰田pcs可以关闭吗_丰田新款卡罗拉变化这么大 让老车主陷入沉思
  9. mysql dml ddl优先级_MYSQL入门操作和常规DML、DDL、DQL使用
  10. 外架小横杆外露长度规范要求_安全文明施工规范