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 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...
随机推荐
- 大数高精度加减乘除 51nod 1005 大数加法
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...
- LPS HDOJ 4745 Two Rabbits
题目传送门 /* 题意:一只兔子顺时针跳,另一只逆时针跳,跳石头权值相等而且不能越过起点 LPS:这道就是LPS的应用,把环倍增成链,套一下LPS,然而并不能理解dp[i][i+n-2] + 1,看别 ...
- Eclipse开发工具介绍
Eclipse是一个基于Java的.开放源码的.可扩展的应用开发平台,它为编程人员提供了一流的Java集成开发环境(Integrated Development Environment,IDE).在E ...
- canvas一周一练 -- canvas绘制中国银行标志(4)
运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...
- HTML meta信息含义
<meta name="viewport" content="width=device-width,initial-scale=1.0"> cont ...
- 03JavaScript运算符与表达式
JavaScript运算符与表达式 2.5运算符与表达式 2.5.1赋值运算符 运算符 意义 运算符 意义 = x=5 /= x=x/y += x=x+y %= 求余赋值 -= x=x-y *= x= ...
- Listview异步加载图片之优化篇
在APP应用中,listview的异步加载图片方式能够带来很好的用户体验,同时也是考量程序性能的一个重要指标.关于listview的异步加载,网上其实很多示例了,中心思想都差不多,不过很多版本或是有b ...
- python多进程和多线程编程
17 多线程和多进程并发 The modules described in this chapter provide support for concurrent execution of code. ...
- Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路题)
题目: The Codejamon game is on fire! Fans across the world are predicting and betting on which team wi ...
- <MySQL>入门五 视图
-- 视图 /* 含义:虚拟表,和普通的表一样使用 mysql5.1版本的新特性,是通过表动态生成的数据,只保存了sql的逻辑,不保存查询的结果 应用场景: - 多个地方用到同样的查询结果 - 该查询 ...