题目:
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<bits/stdc++.h>
using namespace std;
#define MAX 1005
#define NIL 0x3f3f3f3f
int t,mp[MAX][MAX],dis[MAX];
bool vis[MAX];
struct Node
{int x;char str1[5],str2[5];
}node[MAX];
void Dijkstra()
{memset(dis,NIL,sizeof(dis));memset(vis,false,sizeof(vis));dis[0] = 0;priority_queue<int> q;q.push(0);while(!q.empty()){int flag = q.top();q.pop();vis[flag] = true;for(int i = 0;i < t;i++){if(dis[i] > dis[flag] + mp[flag][i]){dis[i] = dis[flag] + mp[flag][i];q.push(i);}}}
}
int main()
{char a[MAX];while(~scanf("%d",&t) && t){for(int i = 0;i < t;i++){cin >> node[i].x >> a;int len = strlen(a);for(int j = 0;j < 4;j++){node[i].str1[j] = a[j];//将单词的前四个字母保存node[i].str2[3 - j] = a[--len];//将单词的后四个字母保存}node[i].str1[4] = '\0';//字符数组结束的标志,一定要加node[i].str2[4] = '\0';}memset(mp,NIL,sizeof(mp));for(int i = 0;i < t;i++){for(int j = 0;j < t;j++){if(!strcmp(node[i].str2,node[j].str1)) mp[i][j] = node[i].x;//找出路径,用邻接矩阵保存}}Dijkstra();//然后就是普通的Dijkstra算法if(dis[t - 1] == 0x3f3f3f3f) cout << "-1" << endl;//这里的结点序号是0到t - 1,所以t - 1是最后一个结点else cout << dis[t - 1] << endl;}return 0;
}

题意:
简单来说,第一行n代表结点个数,之和n行代表由一个数字和一个单词组成,数字代表结点i到其他相连结点的权值,如果一个单词的最后4个字母和另外一个单词的前4个单词完全的相同,那么证明这两点是联通的。
思路:
每次输入时候,用结构体储存,然后把每个单词的前四个字母和最后四个字母保存下来(这里需要做一下处理,具体过程上述代码有注释)。然后我后面用的是Dijkstra算法完成的,只不过这里结点的序号是0到t - 1。

HDU - 1546 Idiomatic Phrases Game相关推荐

  1. HDU 1546 Idiomatic Phrases Game

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

  2. 【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

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

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

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

  4. hdu 1546(最短路)

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

  5. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  8. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. kk_想要学习的知识

    2018/4/27 计算几何 一.简介 计算几何属于ACM算法中比较冷门的分类,在省赛中只在前几年考察过,这两年还没有考过,而且和高精度计算一样,遇到题目主要靠套模板,因此对题意的理解至关重要,而且往 ...

  10. 图论练习题(存起来练)

    =============================以下是最小生成树+并查集======================================  [HDU]  1213 How Man ...

最新文章

  1. js动态创建元素之一--document.write
  2. JDK源码解析 —— IO流中的包装类使用到了装饰者模式
  3. 反思应对焦虑:尽人事 听天命
  4. qt 程序windows 上发布
  5. rabbitmq常用配置
  6. c++模板--2(模板机制,模板的局限性,类模板,类模板做函数的参数)
  7. 全球Python开发者平均年薪5.6万美元,你拖后腿了吗?
  8. python编程(redis操作)
  9. 椭圆极点极线性质_圆锥曲线的统一性质
  10. web.config SetAttributes
  11. php一小时入门,php3小时快速入门-读书笔记
  12. vue-tv-focusable
  13. 怎么在桌面添加便签小工具,win7桌面便签小工具应该怎么添加
  14. 【小程序-开篇】国内IT技术圈的技能树貌似点歪了?
  15. LCD1602A模块的应用
  16. 19级HPU算法协会公开课第一期:【基础算法1】 题解
  17. Linux 基础命令(二)
  18. 论接口自动化测试方法
  19. 安卓11上的存储权限问题
  20. python函数中出现单独的星号和反斜线的涵义

热门文章

  1. iOS开发之制作越狱ios设备ipa包
  2. EasyUi入门教程01
  3. Kindle使用的一些方法
  4. 我的修炼体会--明亭【转】
  5. 42多功能高速闭环驱动器使用手册
  6. 轻运维|无人干预,易捷行云新一代私有云一键式扩容
  7. linux基本权限例子,Linux-3 文件权限-基本权限
  8. 课程表的实现(基于强智科技教务系统)
  9. vmware虚拟机网络配置详解
  10. 使用GO实现尚硅谷家庭记账系统