hdu 1546(dijkstra)
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
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 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.
line for each case. Output an integer indicating the shortest time Tome
will take. If the list can not be built, please output -1.
5 12345978ABCD2341
5 23415608ACBD3412
7 34125678AEFD4123
15 23415673ACC34123
4 41235673FBCD2156
2
20 12345678ABCD
30 DCBF5432167D
0
-1
#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)的更多相关文章
- hdu 1546 Idiomatic Phrases Game
http://acm.hdu.edu.cn/showproblem.php?pid=1546 #include <cstdio> #include <iostream> #in ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1874 Dijkstra算法
先贴个网上找的比较通俗易懂的教程: 2.1Dijkstra算法(非负权,使用于有向图和无向图) Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心 ...
- hdu 1548 (dijkstra解法)(一次AC就是爽)
恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部) <BestCoder用户手册>下载 A strange lift Time Limit: 200 ...
- HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)
题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...
- HDU - 2112 HDU Today Dijkstra
注意: 1.去重边 2.终点和起点一样,应当输出0 3.是无向图 AC代码 #include <cstdio> #include <cmath> #include <al ...
- hdu 1874 dijkstra 队列实现 比数组高效特别在稀疏图
参考 http://blog.csdn.net/zhuyingqingfen/article/details/6370561 刘汝佳白皮书 #include<stdio.h> #incl ...
- 最短路径问题 HDU - 3790 (Dijkstra算法 + 双重权值)
参考:https://www.cnblogs.com/qiufeihai/archive/2012/03/15/2398455.html 最短路径问题 Time Limit: 2000/1000 MS ...
- 最短路 HDU - 2544 (dijkstra算法或Floyd算法)
Dijkstra解法: #include <stdio.h> #include <iostream> #include <cstring> #include < ...
随机推荐
- android studio 首字母提示 设置 大小写敏感
在使用Android studo 编写程序时, 刚开始,关键字提示 首字母 设置了 大小写敏感,小写字母只能提示小写字母开头的,大写字母只能提示大写字母开始的,比较麻烦,在网上搜了下,解决办法如下: ...
- ionic2升级到ionic3并打包APK
通过IONIC2升级到3的时候,经过我一系列的测试,以及网上各种办法,现将新测有效的方法记录如下,本人按如下方法,对多个项目升级后,都能正常打包成APK IONIC 2到3的升级: 1.拷贝ionic ...
- Maven学习 (二) Eclipse 上安装 Maven3插件
eclipse 安装插件的方式最常见的有两种: 1. 一种是在线安装,这貌似是用的最多的,就是:Help --> Install New Software,然后输入 HTTP 地址来安装,但 ...
- centos使用--ssh登陆
1 安装openssh-server yum install openssh-server 2 登录 在配置好ssh后就可以只使用SSH密钥登录而不允许通过密码登录了,方法如下: 修改ssh配置: v ...
- 起始授权机构(SOA)
起始授权机构 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 起始授权机构,SOA(Start Of Authority):该记录表明DNS名称服务器是DNS域中的数据 ...
- Android如何实现毛玻璃效果之Android高级模糊技术
自从iOS系统引入了Blur效果,也就是所谓的毛玻璃.模糊化效果,磨砂效果,各大系统就开始竞相模仿,这是怎样的一个效果呢,我们先来看一下,如下面的图片: 效果我们知道了,如何在Android中实现呢, ...
- 示例解读Java的跨平台原理
首先简单的解释一下Java跨平台的特征,相当于说写一个Java程序论述上可以运行在不同的操作系统平台上面(此处的平台我们就简单的看成是操作系统平台).下面我们用一些事例来说明它的好处. 我们先了解一些 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目9
2014-04-28 23:52 题目:设计算法,找出一本书中某个单词的出现频率. 解法:数就行了. 代码: // 17.9 Given a book, find out the occurrence ...
- 【Soft-Margin Support Vector Machine】林轩田机器学习技术
Hard-Margin的约束太强了:要求必须把所有点都分开.这样就可能带来overfiiting,把noise也当成正确的样本点了. Hard-Margin有些“学习洁癖”,如何克服这种学习洁癖呢? ...
- Tornado详解
1.Tornado路由系统 1.1 Tornado程序示例 新建一个tornadodemo.py, import tornado.ioloop import tornado.web user_info ...