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 < ...
随机推荐
- JavaScript 仿ios滑动选择器
从git上找了一个,不过不是我想要的,更改了许多.到了我想要的效果: roller_selector_0_9.js 首先上js: "use strict"; /* * Author ...
- 把实体bean对象转换成DBObject工具类
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util ...
- java前台传参json,后台用map或者实体对象接收
(一)前台传js对象,里面包含数组,后台用map接收 (1)第一种情况:数组里不包含js对象 var param ={}: param.id=id; param.name=name; var scor ...
- runloop的mode作用是什么?
用来控制一些特殊操作只能在指定模式下运行,一般可以通过指定操作的运行mode来控制执行时机,以提高用户体验 系统默认注册了5个Mode kCFRunLoopDefaultMode:App的默认Mode ...
- Python学习记录一
1.这个在unix类的操作系统才有意义.#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器:#!/usr/bin/env python这种用 ...
- c++知识点总结--new的一些用法
new operator 将对象产生与heap,不但分配内存而且为该对象调用一个constructor operator new只是分配内存,没有constructor被调用 有个一个特殊版本,称 ...
- HDU 3775 Chain Code pick定理
pick定理:一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积. 思路:http://blog.csdn.net ...
- hdu 1203 01背包 I need a offer
hdu 1203 01背包 I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offe ...
- java 计算精度处理
1.设计思想: 定义两个字符串number1和number2,分别输入一串数字,然后定义两个BigDecimal类的对象f1,f2分别接收number1和number2的值,然后调用BigDecima ...
- PHP session 与cookie
知识点: session是将服务器将网页产生的会话信息以数组形式存到一个php文件中,产生的全局变量,可以在系统下的其他网页任意调用这个数据. cookie类似于session原理,但是是将数据存给用 ...