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 < ...
随机推荐
- springboot遇见问题总结
今天开始学习创建springboot项目 问题1: 产生异常: 创建项目目录: demo代码: 代码Controller import org.springframework.web.bind.ann ...
- “管中窥豹”,MyCAT的基因缺陷
提起MyCAT,我的脑海里,总是浮现出这样一首偈.弘忍觉得自己老了,需要找一个接班人,于是,弘忍要求弟子们每人写一个偈子,根据偈子观察各位弟子的开悟程度,从而传授正宗禅宗的衣钵.弟子们心里其实都很明白 ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目8
2014-03-18 02:12 题目:判断一个字符串是否由另一个字符串循环移位而成. 解法:首先长度必须相等.然后将第一个串连拼两次,判断第二个串是否在这个连接串中. 代码: // 1.8 Assu ...
- 玩转Node.js(一)
玩转Node.js(一) 在说Node.js之前,我们先来说说js,如果你也曾开发过前端,那么你一定接触到了这个叫JavaScript有趣的东西,而对于JavaScript,你只会基本的操作——为we ...
- ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor 【转】
原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-monitor-in-asp-net-core. ...
- linux备忘录-文件系统管理
Extx 文件系统原理 block group 每个分区(partition)的组成为 boot sector -> block group -> block group -> bl ...
- 使用pdb模块调试Python
在Python中,我们需要debug时,有三种方式: 加log语句.最简单的方式是添加print()语句来输出我们想要获知的状态或者变量,好处是简单容易操作,坏处是debug完了之后,还需要将prin ...
- [AGC005D] ~K Perm Counting [dp]
题面 传送门 思路 首先可以明确的一点是,本题中出现不满足条件的所有的数,都是分组的 只有模$K$意义下相同的数之间才会出现不满足条件的情况,而且仅出现在相邻的情况 那么我们考虑把这个性质利用起来 我 ...
- 2013年 ACMICPC 杭州赛区H题
思路:树状数组统计.待验证,不知道是否对. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- 牛客 NOIp模拟1 T1 中位数 解题报告
中位数 题目描述 小\(N\)得到了一个非常神奇的序列\(A\).这个序列长度为\(N\),下标从\(1\)开始.\(A\)的一个子区间对应一个序列,可以由数对\([l,r]\)表示,代表\(A[l] ...