Idiomatic Phrases Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3154    Accepted Submission(s): 1026

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组"成语",每个成语都有其权值,对于任意两个成语,如果第一个成语的后4个字符能够与另外一个成语的前4个字符匹配,则这两个成语是可以接上去的,问从第一个成语开始,能否找到一条接的顺序,让第一个成语能够接到最后一个.如果能,输出接到最后一个所花费的最小代价,不能够则输出-1.
题解:将每个成语看成一个点,能够接上去则连上一条边。然后dijkstra算法进行求解.
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
const int INF = ;
struct Node{
char s[],e[];
int w;
}node[N];
int graph[N][N];
int n;
void deal(char s[],int id){
for(int i=;i<;i++) node[id].s[i] = s[i];
int len = strlen(s);
int k =;
for(int i=len-;i<len;i++) {
node[id].e[k++] = s[i];
}
}
int low[N];
bool vis[N];
void dijkstra(int s){
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++){
low[i] = (i==s)?:graph[s][i];
}
vis[s] = true;
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<n;j++){
if(Min>low[j]&&!vis[j]){
s=j;
Min = low[j];
}
}
vis[s] = true;
for(int j=;j<n;j++){
if(low[j]>low[s]+graph[s][j]&&!vis[j]){
low[j]=low[s]+graph[s][j];
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i==j) graph[i][j] = ;
else graph[i][j]=INF;
}
}
for(int i=;i<n;i++){
char str[N];
scanf("%d%s",&node[i].w,str);
deal(str,i);
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(strcmp(node[i].e,node[j].s)==) graph[i][j] = node[i].w;
}
}
dijkstra();
if(low[n-]>=INF) printf("-1\n");
else printf("%d\n",low[n-]);
}
return ;
}

hdu 1546(dijkstra)的更多相关文章

  1. hdu 1546 Idiomatic Phrases Game

    http://acm.hdu.edu.cn/showproblem.php?pid=1546 #include <cstdio> #include <iostream> #in ...

  2. HDU 2112 HDU Today (Dijkstra算法)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. hdu 1874 Dijkstra算法

    先贴个网上找的比较通俗易懂的教程: 2.1Dijkstra算法(非负权,使用于有向图和无向图) Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心 ...

  4. hdu 1548 (dijkstra解法)(一次AC就是爽)

    恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部) <BestCoder用户手册>下载 A strange lift Time Limit: 200 ...

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

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

  6. HDU - 2112 HDU Today Dijkstra

    注意: 1.去重边 2.终点和起点一样,应当输出0 3.是无向图 AC代码 #include <cstdio> #include <cmath> #include <al ...

  7. hdu 1874 dijkstra 队列实现 比数组高效特别在稀疏图

    参考  http://blog.csdn.net/zhuyingqingfen/article/details/6370561 刘汝佳白皮书 #include<stdio.h> #incl ...

  8. 最短路径问题 HDU - 3790 (Dijkstra算法 + 双重权值)

    参考:https://www.cnblogs.com/qiufeihai/archive/2012/03/15/2398455.html 最短路径问题 Time Limit: 2000/1000 MS ...

  9. 最短路 HDU - 2544 (dijkstra算法或Floyd算法)

    Dijkstra解法: #include <stdio.h> #include <iostream> #include <cstring> #include < ...

随机推荐

  1. Android 图片放错位置会拉伸变形

    今天做了一个很小的需求,然后需要图片,我给ui要图片.直接给了我三套,还命名 x . xx. 2k 真的一开始都不知道.没有玩过这么正规的.我就用了一张,放到了hdpi下面. 后来同事帮我才知道, 图 ...

  2. curl 编译

    curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本.支持很多协议:FTP, FTPS, HT ...

  3. Maven学习 (二) Eclipse 上安装 Maven3插件

    eclipse 安装插件的方式最常见的有两种: 1. 一种是在线安装,这貌似是用的最多的,就是:Help  -->  Install New Software,然后输入 HTTP 地址来安装,但 ...

  4. eclipse、myeclipse 反编译插件 轻松查看源代码

    java反编译插件:Eclipse Class Decompiler,能够很方便的使用本插件查看类库源码,以及采用本插件进行Debug调试. eclipse中安装Eclipse Class Decom ...

  5. 《Cracking the Coding Interview》——第17章:普通题——题目11

    2014-04-29 00:00 题目:给定一个rand5()函数,能够返回0~4间的随机整数.要求实现rand7(),返回0~6之间的随机整数.该函数产生随机数必须概率相等. 解法:自己想了半天没想 ...

  6. Asp.Net中Response.Cookies.Remove 无法删除COOKIE的问题解决方法

    登陆功能经常需要使用Cookie来存储登陆信息,可是在开发过程中,经常发现cookie无法删除的问题.删除的代码无非就是找到Cookie并删除掉. 但是会发现 Response.Cookies.Rem ...

  7. [转载]unity优化1

    官方优化文档--优化图像性能http://docs.unity3d.com/Documentation/Manual/OptimizingGraphicsPerformance.html Unity3 ...

  8. ASP.Net MVC+EF架构

    ASP.Net MVC是UI层的框架,EF是数据访问的逻辑. 如果在Controller中using DbContext,把查询的结果的对象放到cshtml中显示,那么一旦在cshtml中访问关联属性 ...

  9. Python——数据类型初步:Numbers

    本篇内容 今天主要简介了几种数字的数据类型和一些稍微比较常用的方法. • int • bytes • float • bool • complex • long Python里面的使用变量的时候并不需 ...

  10. Python的HttpClient实现

    Python版本3.4(注意python的版本,python2和python3的写法不一样) 其实无非就是客户端的请求,所以python中这里使用的是urllib.request模块.主要注意的是he ...