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 < ...
随机推荐
- CentOS 7.X 设置系统时间
在CentOS 6版本,时间设置有date.hwclock命令, 硬件时钟和系统时钟 (1) 硬件时钟 RTC(Real-Time Clock)或CMOS时钟,一般在主板上靠电池供电,服务器断电后也会 ...
- 使用Subversion进行源代码管理(二):创建和发布版本库[转]
原文出处: http://www.blogjava.net/youxia/archive/2007/10/23/155372.html 我的上一篇随笔讲了怎么使用Subversion客户端去连接服务器 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目3
2014-04-25 19:42 题目:C++中虚函数的工作原理? 解法:虚函数表?细节呢?要是懂汇编我就能钻的再深点了.我试着写了点测大小.打印指针地址之类的代码,能起点管中窥豹的作用,从编译器的外 ...
- webdriver--设置元素等待
sleep():脚本执行到某一位置时“睡一会”,再继续执行:参数的单位是s:sleep方法由python的time模块提供,有两种引入和使用方式 import time time.sleep(5) f ...
- ADB命令总结(1)
今日继续学习ADB,使用真机来操作,因此把所用到的命令总结如下: 一,模拟按HOME键 adb shell input keyevent KEYCODE_HOME 二,滑动手机屏幕 从(x1,y1)滑 ...
- Python 3基础教程2-打印语句和字符串
本文介绍Python 3中的打印语句和字符串使用,具体练习请看下面的demo.py print ('Hello Python 3!') """文本讲打印语句和字符串打印语 ...
- SDK接入注意点
1. 新建的android项目,要把MainActivity.java里生成的东西全部删去,最好只留个onCreate入口方法,不然会产生什么“hello world”,会把自己写的View内的东西覆 ...
- windows版本cloudbase-init流程说明
源码流程说明 - 程序首先判断操作系统类型,加载对应的模块 - 加载服务,服务共分为四种: 'cloudbaseinit.metadata.services.httpservice.HttpServi ...
- Elasticsearch自定义分析器
关于分析器 ES中默认使用的是标准分析器(standard analyzer).如果需要对某个字段使用其他分析器,可以在映射中该字段下说明.例如: PUT /my_index { "mapp ...
- Struts2+DAO层实现实例02——搭建DAO基本框架并与Struts2组合
实例内容 创建DAO(Data Access Oject)接口:BaseDAO 创建其实例化类:UserDAO 用于获取数据库struts中的userinfo表中的内容 创建User的Java Bea ...