HDU 1546 Idiomatic Phrases Game 求助!help!!!
Idiomatic Phrases Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4000 Accepted Submission(s):
1363
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.
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.
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<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
using namespace std;
int n,tot;
queue<int>que;
char s[][];
int dis[MAXN],vis[MAXN],num[MAXN];
int to[MAXN],net[MAXN],cap[MAXN],head[MAXN];
void add(int u,int v,int w){
to[++tot]=v;cap[tot]=w;net[tot]=head[u];head[u]=tot;
}
int judge(int u,int v){
int len1=strlen(s[u]);
int len2=strlen(s[v]);
for(int i=,j=len2-;i<,j<=len2-;i++,j++)
if(s[u][j]!=s[v][i]) return false;
return true;
}
void spfa(){
memset(vis,,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
while(!que.empty()) que.pop();
que.push();vis[]=;dis[]=;
while(!que.empty()){
int now=que.front();
que.pop();vis[now]=;
for(int i=head[now];i;i=net[i])
if(dis[to[i]]>dis[now]+cap[i]){
dis[to[i]]=dis[now]+cap[i];
if(!vis[to[i]]){
vis[to[i]]=;
que.push(to[i]);
}
}
}
}
int main(){
while(scanf("%d",&n)&&n!=){
for(int i=;i<=n;i++) cin>>num[i]>>s[i];
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(judge(i,j)) add(i,j,num[i]);
spfa();
if(dis[n]<) cout<<dis[n]<<endl;
else cout<<"-1"<<endl;
tot=;
memset(cap,,sizeof(cap));
memset(head,,sizeof(head));
}
}
/*
5
5 12345978ABCD2341
5 23415608ACBD3412
7 34125678AEFD4123
15 23415673ACC34123
4 41235673FBCD2156
2
20 12345678ABCD
30 DCBF5432167D
5
5 12345978ABCD1234
5 23415608ACBD3412
7 34125678AEFD4123
5 41235673FBCD1234
5 12345978ABCD1234
1
5 123456781234
0
*/
HDU 1546 Idiomatic Phrases Game 求助!help!!!的更多相关文章
- hdu 1546 Idiomatic Phrases Game
http://acm.hdu.edu.cn/showproblem.php?pid=1546 #include <cstdio> #include <iostream> #in ...
- HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)
题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...
- HDU - 1546 ZOJ - 2750 Idiomatic Phrases Game 成语接龙SPFA+map
Idiomatic Phrases Game Tom is playing a game called Idiomatic Phrases Game. An idiom consists of sev ...
- hdu 1546(dijkstra)
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Idiomatic Phrases Game(图论最短路)
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Idiomatic Phrases Game(最短路+注意坑点)
Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...
- ZOJ 2750 Idiomatic Phrases Game(Dijkstra)
点我看题目 题意 : 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的 ...
- zoj 2750 Idiomatic Phrases Game
迪杰斯特拉单源最短路算法.对成语进行预处理.做出邻接矩阵即可. #include<cstdio> #include<cstring> #include<cmath> ...
- ZOJ-2750 Idiomatic Phrases Game---Dijk最短路
题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...
随机推荐
- 暴力/思维 HDOJ 5386 Cover
题目传送门 /* 题意:给出刷墙的所有的方法,求一种顺序,使得原矩阵刷成目标矩阵 暴力:(题解)我们只要每次找一行或一列颜色除了0都相同的,然后如果有对应的操作,就把这行这列都赋值成0即可 */ /* ...
- gauge自动化测试框架(二)
Gauge通过Markdown来描述行为,所以我还需先学习学习Markdown语法,看了一下,还好,十来分钟应该就可以搞定.具体学习见另一篇文章
- 基本的查询流【MSSQL】
4个DML(Data Manipulation Language)命令 SELECT INSERT UPDATE DELETE 查询语法有一个特有的固定顺序 SELECT - FROM - WHERE ...
- Sql2008事务日志已满处理
处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...
- Java使用 POI 操作Excel
Java中常见的用来操作 Excel 的方式有2种:JXL和POI.JXL只能对 Excel进行操作,且只支持到 Excel 95-2000的版本.而POI是Apache 的开源项目,由Java编写的 ...
- Python基础:基本数据类型
python基本标准6类数据类型:Number数字, String字符串, List列表,Tuple元组,Set集合,Dictionary字典 不可变数据3个(Number数字,String字符串,T ...
- Android 百度地图 android.view.InflateException: Binary XML file line Error inflating class com.baidu.mapapi.map.MapView
android.view.InflateException: Binary XML file line Error inflating class com.baidu.mapapi.map.MapVi ...
- 配置服务器 Ubuntu 记录+踩坑
从零开始配置服务器用于ss+站点 1. SS 首先安装pyenv,安装pyenv之前先安装必要环境,具体命令行请见: https://github.com/pyenv/pyenv/wiki/Commo ...
- 【百度编辑器ueditor】工具,如何去掉百度编辑器 ueditor 元素路径、字数统计等
去掉如下截图: 在百度编辑器 ueditor 根目录下: ueditor.config.js 文件中 搜索并将参数elementPathEnabled设置成false即可 常用功能开关如下: ,ele ...
- Codeforces_776_C_(思维)(前缀和)
C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input stand ...