Idiomatic Phrases Game(图论最短路)
Idiomatic Phrases Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3504 Accepted Submission(s): 1182
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
Sample Output
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int INF = 0x6f6f6f6f;
int n;
struct Idiom
{
int spend;
char str[];//好坑,50反正是错的,100才对,成语原来还有这么多字
}idiom[]; int spe[][]; int vis[],dis[];
void Dijkstra()
{
int i,j,mmm,m;
memset(vis,,sizeof(vis));
memset(dis,0x6f,sizeof(dis));
dis[] = ;
for(i = ; i<=n; i++)
{
mmm = INF;
for(j = ; j<=n; j++)
{
if(dis[j]<mmm &&!vis[j])
{
m = j;
mmm = dis[j];
}
}
vis[m] = ;
for(j = ; j<=n; j++)
{
if(dis[m]+spe[m][j]<dis[j]&&!vis[j])
dis[j] = dis[m]+spe[m][j];
}
}
} int main()
{
int i,j,len;
while(scanf("%d",&n)&&n)
{
for(i = ; i<=n; i++)
scanf("%d%s",&idiom[i].spend,idiom[i].str);
for(i = ; i<=n; i++)
{
len = strlen(idiom[i].str);
for(j = ; j<=n; j++)
{
if(idiom[i].str[len-]==idiom[j].str[]&&
idiom[i].str[len-]==idiom[j].str[]&&
idiom[i].str[len-]==idiom[j].str[]&&
idiom[i].str[len-]==idiom[j].str[])
spe[i][j]=idiom[i].spend;
else
spe[i][j]=INF;
}
}
Dijkstra();
if (dis[n]!=INF)
printf("%d\n",dis[n]);
else
printf("-1\n");
}
return ;
}
Idiomatic Phrases Game(图论最短路)的更多相关文章
- HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)
题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...
- Idiomatic Phrases Game(最短路+注意坑点)
Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...
- HDU - 1546 ZOJ - 2750 Idiomatic Phrases Game 成语接龙SPFA+map
Idiomatic Phrases Game Tom is playing a game called Idiomatic Phrases Game. An idiom consists of sev ...
- HDU 1546 Idiomatic Phrases Game 求助!help!!!
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- ZOJ-2750 Idiomatic Phrases Game---Dijk最短路
题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- 图论(最短路&最小生成树)
图论 图的定义与概念 图的分类 图,根据点数和边数可分为三种:完全图,稠密图与稀疏图. 完全图,即\(m=n^2\)的图\((m\)为边数,\(n\)为点数\()\).如: 1 1 0 1 2 1 1 ...
- D1图论最短路专题
第一题:poj3660 其实是Floyed算法的拓展:Floyd-Wareshall.初始时,若两头牛关系确定则fij = 1. 对于一头牛若确定的关系=n-1,这说明这头牛的排名是确定的. 通过寻找 ...
- 图论最短路——spfa
今天开始图论的最短路的最后复习,今天自己手打spfa虽然看了一眼书,但是也算是自己打出来的,毕竟很久没打了,而且还是一遍a代码下来15min左右就搞完了,成就感++.所以呢来篇博客记录一下. 香甜的黄 ...
随机推荐
- Shell--变量的显示与设置、环境变量、语系变量
1.变量的显示与设置:echo,unsetecho:显示一段文字,也可以读出变量内容并打印出来 格式echo $变量或者echo ${变量}语 法:echo [-neE][字符串]或 echo [- ...
- display:inline-block;在各浏览器下的问题和终极兼容办法
display:inline-block;在各浏览器下的问题和终极兼容办法 一.IE 5.5.6.7 .8(Q)中display:inline-block;失效 兼容办法: IE 5.5.6.7 .8 ...
- Spring注释事务失效及解决办法
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 在主容器中(applicationContext.xml),将C ...
- Exception thrown in catch and finally clause
Based on reading your answer and seeing how you likely came up with it, I believe you think an " ...
- [转载]Process工具类,提供设置timeout功能
FROM:http://segmentfault.com/blog/lidonghao/1190000000372535 在前一篇博文中,简单介绍了如何使用Process类来调用命令行的功能,那样使用 ...
- Windows网页上碰到无法完全显示的图片怎么办
如图所示,下一幅图片只能显示一半. 我们选中并在新标签中打开 一般即可正常显示,如果还不行,留意地址栏,这就是这个图片的真实地址,我们完全可以用迅雷直接把这幅图片下载下来. 我们甚至可以猜测,去掉后缀 ...
- openlayers对接百度地图新方法
上次给大家提供的openlayers对接百度地图有些问题,是因为没有进行分辨率设置,也没有进行相应的平面坐标转换,获取getURL的方法还是没有变化的 getURL: function (bounds ...
- vuex mapMutations 使用
你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit ...
- 怎么以最新汇率牌价计算XX美元相当于多少人民币
http://www.meiguozhuji.com/exchange-rate 美国主机都是以美元来报价的,至于XX美元相当于多少人民币,很多朋友都不太清楚.为了让大家更直接的了解购买美国主机需要花 ...
- java计算时间差 Java问题通用解决代码
java实现计算时间差 正式版: /** * 计算时间差,求出两者相隔的时间 * * @param nowDate * ...