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. ECSHOP搜索框文字点击消失

    <input name="keywords" type="text" id="keyword" value="黄山金银币&q ...

  2. 6. datasource - mysql【从零开始学Spring Boot】

    在任何一个平台都逃离不了数据库的操作,那么在spring boot中怎么接入数据库呢? 很简单,我们需要在application.properties进行配置一下,application.proper ...

  3. hdu 5284 wyh2000 and a string problem(没有算法,仅仅考思维,字符数组得开20万,不然太小了)

    代码: #include<cstdio> #include<cstring> using namespace std; char s[200000]; int main() { ...

  4. Windows为什么双击打开‘我的电脑’, 没有了‘前进’‘ 后退’‘向上’等按钮?

    如图所示   点击查看 工具栏 标准按钮即可   左侧的数值虚线可以拖动到任意,还可以添加按钮如搜索,删除,复制,剪切等

  5. SQL Server“吃内存”的解决

    现象:Web服务器中SQL Server占用内存非常高,加内存后,SQL Server又吃掉新加的内存,好像内存永远不够用一样. 分析:其实这并不一定是由于SQL Server活动过度造成的,在启动S ...

  6. 【转载】json对象的使用

    使用JSON 进行数据传输 一.选择的意义 在异步应用程序中发送和接收信息时,可以选择以纯文本和 XML 作为数据格式.为了更好的使用ajax, 我们将学习一种有用的数据格式 JavaScript O ...

  7. Mysql视图的创建及使用

    视图理解: 视图又叫虚表.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成. 视 ...

  8. Java自动内存管理机制

    1.运行时数据区域划分 2.程序计数器 作用:可以看做是当前线程所执行的字节码的行号指示器. 解释:字节码指示器就是通过改变程序计数器的值来指定下一条需要执行的指令.分支,循环等 基础功能就是依赖程序 ...

  9. Android 函数回调

    1 http://blog.csdn.net/xyz_lmn/article/details/8631195 我感觉fragment和activity的通信形象的解释了函数的回调,看别人的博客越看越迷 ...

  10. centos7 重启网卡报错

    systemctl restart network 时候报错: rtnetlink answers file exists 是network和NetworkManager冲突了 一般建议直接 syst ...