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

Problem Description
Tom 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
The 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.
 
Output
One line for each case. Output an integer indicating
the shortest time Tome will take. If the list can not be built, please output
-1.
 
Sample Input
5
5 12345978ABCD2341
5 23415608ACBD3412
7 34125678AEFD4123
15 23415673ACC34123
4 41235673FBCD2156
2
20 12345678ABCD
30 DCBF5432167D
0
 
Sample Output
17
-1
 
Author
ZHOU, Ran
 
Source
 
Recommend
linle   |   We have carefully selected several similar
problems for you:  1548 1317 1535 3339 1217 
思路:很简单的spfa,但是wa了好多次,而且不知道哪里错了,望大佬不吝赐教。
#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!!!的更多相关文章

  1. hdu 1546 Idiomatic Phrases Game

    http://acm.hdu.edu.cn/showproblem.php?pid=1546 #include <cstdio> #include <iostream> #in ...

  2. HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)

    题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...

  3. 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 ...

  4. hdu 1546(dijkstra)

    Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. Idiomatic Phrases Game(图论最短路)

    Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  6. Idiomatic Phrases Game(最短路+注意坑点)

    Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...

  7. ZOJ 2750 Idiomatic Phrases Game(Dijkstra)

    点我看题目 题意 : 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的 ...

  8. zoj 2750 Idiomatic Phrases Game

    迪杰斯特拉单源最短路算法.对成语进行预处理.做出邻接矩阵即可. #include<cstdio> #include<cstring> #include<cmath> ...

  9. ZOJ-2750 Idiomatic Phrases Game---Dijk最短路

    题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...

随机推荐

  1. [C和指针] 1-快速上手、2-基本概念、3-数据

    第1章 快速上手 1.1.1 空白和注释   程序的空白的作用: 空行将程序的不同部分分割开来:制表符缩进语句,可以更好地显示程序的结构等等.     软件最大的开销并非在于编写,而是在于维护,所以需 ...

  2. CF482C Game with Strings

    题意 你和你的朋友玩一个游戏,游戏规则如下. 你的朋友创造 n 个长度均为 m 的不相同的字符串,然后他随机地选择其中一个.他选择这些字符串的概率是相等的,也就是说,他选择 n 个字符串中的每一个的概 ...

  3. 二分搜索poj106

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49944   Accepted: 10493 De ...

  4. linux学习之路4 系统目录架构

    linux树状文件系统结构 bin(binary) 保存可执行文件 也就是保存所有命令 boot 引导目录 保存所有跟系统有关的引导程序 其中Vmlinux文件最为重要,是系统内核 dev 保存所有的 ...

  5. php 页面展示

    php 页面展示 复杂逻辑. 段落注释

  6. (四)Mybatis总结之接口映射

    前面Mybatis是直接通过Dao层与数据交互,更好的方法是Mybatis通过接口映射方式与数据交互 1.在项目中添加maven支持(即pom.xml下添加支持) <!-- 在pom.xml下配 ...

  7. Android电池电量跳变

    高通平台8916/8917 对于第三方Fuel Gauge,跳变多是因为IC内部算法的问题,这样我们可以通过驱动来规避. 例如:usb在位时,要阻止电量的向下跳变. 当电量越级跳变时,要在驱动中能检测 ...

  8. mongo 3.4分片集群系列之八:分片管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  9. ci框架中model简单的mysql操作

    <?php class SingerModel extends CI_Model { function SingerModel() { //会将数据库对象赋值给CI_Controller的db属 ...

  10. (转) 淘淘商城系列——Redis五种数据类型介绍

    http://blog.csdn.net/yerenyuan_pku/article/details/72855562 Redis支持五种数据类型:string(字符串),hash(哈希),list( ...