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
*/

最新文章

  1. 让Lua支持Linq吧
  2. Java_jvisualvm使用JMX连接远程机器(实践)
  3. MFC操作注册表
  4. eclipse安装genymotion插件。
  5. ubuntu apt-get 时 Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
  6. ABAP 内表的行列转换-发货通知单-打印到Excel里-NEW
  7. &amp;是什么运算符(转)
  8. Java8新特性 1——利用流和Lambda表达式操作集合
  9. 一个PHP书单 -摘自网络
  10. VC++中操作XMLWin32实例
  11. centos上安装rabbitmq并且python测试
  12. mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接解决方法
  13. iPhone6设计自适应布局
  14. jquery.validate.js之自定义表单验证规则
  15. C# 后台通过网络地址访问百度地图取回当前在地图上的经纬度,并将取回的复杂Json格式字符串反序列化(Newtonsoft.Json)
  16. Deep Learning.ai学习笔记_第三门课_结构化机器学习项目
  17. 使用POI读取xlsx文件,包含对excel中自定义时间格式的处理
  18. geckodriver问题
  19. learning uboot part command
  20. sgu 122. The book 满足ore性质的汉密尔顿回路 难度:2

热门文章

  1. kafka启动时出现FATAL Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer) java.io.IOException: Permission denied错误解决办法(图文详解)
  2. python中os模块中文帮助
  3. Codeforces_766_D_(并查集)
  4. 简单工厂模式&amp;工厂方法模式&amp;抽象工厂模式的区别
  5. CAD得到范围内实体(网页版)
  6. 梦想CAD控件关于曲线问题
  7. CMU-准备
  8. 浮动和margin负值 三列布局
  9. &lt;MyBatis&gt;入门一 HelloWorld
  10. MyBatis 的基本要素—SQL 映射文件