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

5
5 12345978ABCD2341
5 23415608ACBD3412
7 34125678AEFD4123
15 23415673ACC34123
4 41235673FBCD2156
2
20 12345678ABCD
30 DCBF5432167D
0

Sample Output

17
-1
 
 
//有多组输入数据,第一行 n 表示有 n 个成语,每个成语需要 T 的时间去找到下一个 ,然后是成语,用字符串表示,4个字符是一个汉字
成语接龙,从第一个接到最后一个需要多少时间
Dijkstra算法
 #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(图论最短路)的更多相关文章

  1. HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)

    题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...

  2. Idiomatic Phrases Game(最短路+注意坑点)

    Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...

  3. 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 ...

  4. HDU 1546 Idiomatic Phrases Game 求助!help!!!

    Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. ZOJ-2750 Idiomatic Phrases Game---Dijk最短路

    题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...

  6. HDU 5521 [图论][最短路][建图灵感]

    /* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...

  7. 图论(最短路&最小生成树)

    图论 图的定义与概念 图的分类 图,根据点数和边数可分为三种:完全图,稠密图与稀疏图. 完全图,即\(m=n^2\)的图\((m\)为边数,\(n\)为点数\()\).如: 1 1 0 1 2 1 1 ...

  8. D1图论最短路专题

    第一题:poj3660 其实是Floyed算法的拓展:Floyd-Wareshall.初始时,若两头牛关系确定则fij = 1. 对于一头牛若确定的关系=n-1,这说明这头牛的排名是确定的. 通过寻找 ...

  9. 图论最短路——spfa

    今天开始图论的最短路的最后复习,今天自己手打spfa虽然看了一眼书,但是也算是自己打出来的,毕竟很久没打了,而且还是一遍a代码下来15min左右就搞完了,成就感++.所以呢来篇博客记录一下. 香甜的黄 ...

随机推荐

  1. 淘宝开源项目之Tsar

    软件介绍: Tsar是淘宝开发的一个非常好用的系统监控工具,在淘宝内部大量使用,它不仅可以监控CPU.IO.内存.TCP等系统状态,也可以监控Apache,Nginx/Tengine,Squid等服务 ...

  2. django 用model来简化form

    django里面的model和form其实有很多地方有相同之处,django本身也支持用model来简化form 一般情况下,我们的form是这样的 from django import forms ...

  3. 在c++代码中执行bat文件 【转】

    我想在c++代码中执行磁盘上的一个bat文件. 这个bat文件的完整路径是:E:\\7z\\my7z.bat. 方法一: system("E:\\7z\\my7z.bat"); s ...

  4. EL表达式中的empty运算符

  5. ASIHttpRequest请求时的默认编码

    在ASIHttpRequest.m文件 中的 - (id)initWithURL:(NSURL *)newURL方法中找到 [self setDefaultResponseEncoding:NSISO ...

  6. [Angular] Debug Angular apps in production without revealing source maps

    Source: https://blog.angularindepth.com/debug-angular-apps-in-production-without-revealing-source-ma ...

  7. 使用HttpClient测试SpringMVC的接口

    转载:http://blog.csdn.net/tmaskboy/article/details/52355591 最近在写SSM创建的Web项目,写到一个对外接口时需要做测试,接受json格式的数据 ...

  8. 尝试一下markdown

    尝试一下markdown 简单介绍以下几个宏: __VA_ARGS__是一个可变参数的宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持).宏前面加上##的 ...

  9. liunx下安装第三方Python(PIP安装)

    wget https://pypi.python.org/packages/source/p/pip/pip-6.0.8.tar.gz $ tar zvxf pip-6.0.8.tar.gz $ cd ...

  10. Laravel之命令

    一.创建命令 php artisan make:console SendEmails 上述命令将会生成一个类app/Console/Commands/SendEmails.php,当创建命令时,--c ...