题目链接

Idiomatic Phrases Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

【题意】

成语接龙,每个成语由4个16进制数组成,看是否能从第一个成语接到最后一个成语,接每个成语需要翻字典有耗时,如果能接到最后一个成语,则输出最少耗时,反之输出-1。

【解题思路】

将首尾能对应成语建边,权值为时间,然后用dijkstra跑一遍最短路。如果dis[n-1]=INF,说明没有能接到最后一个成语,所以输出-1,否则输出最短路。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int INF=999999999;
const int maxn=1005;
int edge[maxn][maxn],vis[maxn],dis[maxn];
char s[maxn];
int n;
struct Node
{char a[5],b[5];int time;
}node[maxn];
void dijkstra()
{for(int i=0;i<n;i++)dis[i]=INF;dis[0]=0;while(1){int v=-1;for(int i=0;i<n;i++){if(!vis[i] && (v==-1 |dis[i]<dis[v]))v=i;}if(v==-1)break;vis[v]=1;for(int i=0;i<n;i++)dis[i]=min(dis[i],dis[v]+edge[v][i]);}
}
int main()
{while(~scanf("%d",&n) && n){for(int i=0;i<n;i++){scanf("%d %s",&node[i].time,s);int l=strlen(s);for(int k=0,j=l-1;k<4;j--,k++){node[i].a[k]=s[k];node[i].b[3-k]=s[j];}node[i].a[4]=node[i].b[4]='\0';}for(int i=0;i<n;i++){for(int j=0;j<n;j++){edge[i][j]=INF;if(i==j)continue;if(strcmp(node[i].b,node[j].a)==0)edge[i][j]=node[i].time;}}memset(vis,0,sizeof(vis));dijkstra();if(dis[n-1]==INF)printf("-1\n");else printf("%d\n",dis[n-1]);}return 0;
}

【ZOJ2750】Idiomatic Phrases Game(最短路)相关推荐

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

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

  2. HDU 1546 Idiomatic Phrases Game

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

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

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

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

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

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

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

  6. kk_想要学习的知识

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

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

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

  8. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

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

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

  10. 【POJ - 3037】Skiing (Dijkstra算法)

    题干: Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Be ...

最新文章

  1. linux数组删除数据,JavaScript在数组的循环中删除元素
  2. halcon与c#联合编程的方法
  3. 我诞生了!祝贺我吧。
  4. 远程 不显示本地硬盘解决办法(高级版)
  5. python中break、continue 、exit() 、pass终止循环的区别
  6. 我的文档目录环境变量_从.env文件中为NodeJS加载环境变量
  7. SpatialHadoop中空间索引系列之(四)空间格网索引实现
  8. QPushButton hover配置
  9. mysql优化 坑_mysql之我们终将踩过的坑(优化)
  10. mysql+join+合计_图解MySQL里的各种 JOIN,看完不懂来找我!
  11. 4. Linux 高性能服务器编程 --- TCP/IP 通信
  12. 一行代码迁移TensorFlow 1.x到TensorFlow 2.0
  13. 英伟达发布《永劫无间》最新显卡驱动更新,驱动人生升级教程
  14. 目标检测标签分配之 OTA 和 SimOTA 细节学习
  15. Codeforces Gym 100015B Ball Painting 找规律
  16. 力扣刷题 DAY_70 回溯
  17. 华为交换机ARP防网关冲突
  18. HAproxy增加日志记录功能和自定义日志输出内容、格式
  19. 数独的随机终盘生成函数设计
  20. 2000/XP/2003常见问题集锦

热门文章

  1. android 7 uc flash player,大神教你解决win7系统uc浏览器提示flash player遭到阻止的详细办法...
  2. CESIUM例子学习(一)——动态模型加载
  3. 在word表格中如何快速清除表格内容?
  4. VC++ excel 写入与写出数据
  5. 融云对接php,谈谈微信小程序中首次对接融云WebIM SDK经验
  6. 最全面的 Fiddler 教程讲解
  7. android内核函数,Android display架构分析三-Kernel Space Display架构介绍
  8. Apache安全漏洞
  9. HTML5实现点击触发灯泡开关
  10. prompt learning——你需要掌握的基础知识以及离散型 prompt 的代码