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. 

InputThe 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. 
OutputOne 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 这个题的坑点在字符串的长度上
代码:
vector版
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e5+;
typedef long long ll;
using namespace std;
char str[][];
int w[];
struct node
{
int pos;
int w;
node (int x,int y)
{
pos=x;
w=y;
}
bool friend operator < (node x,node y)
{
return x.w>y.w;
}
};
vector<node>vec[];
int dis[];
int vis[];
int n,m;
void init()
{
for(int t=;t<=n;t++)
{
dis[t]=Inf;
}
memset(vis,,sizeof(vis));
} void Dijkstra(int s)
{
priority_queue<node>q;
q.push(node(s,));
dis[s]=;
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.pos])continue;
vis[now.pos]=;
for(int t=;t<vec[now.pos].size();t++)
{
node to=vec[now.pos][t];
if(to.w+dis[now.pos]<dis[to.pos])
{
dis[to.pos]=to.w+dis[now.pos];
to.w=dis[to.pos];
q.push(to);
}
}
}
}
bool ok(int i, int j)
{
int len=strlen(str[i]);
if(str[i][len-] == str[j][] && str[i][len - ] == str[j][] && str[i][len-] == str[j][] && str[i][len-] == str[j][]) {
return true;
}
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
{
break;
}
init();
for(int t=;t<=n;t++)
{
vec[t].clear();
}
for(int t=;t<=n;t++)
{
scanf("%d %s",&w[t],str[t]);
}
for(int t=;t<=n;t++)
{
for(int j=;j<=n;j++)
{ if(ok(t,j))
vec[t].push_back(node(j,w[t])); }
}
Dijkstra();
if(dis[n]!=Inf)
printf("%d\n",dis[n]);
else
{
puts("-1");
}
}
return ;
}

邻接表版

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
#define Inf 0x3f3f3f3f
const int maxn=1e5+;
typedef long long ll;
using namespace std;
struct edge
{
int u,v,w;
int next;
}Edge[*maxn];
int w[];
char str[][];
struct node
{
int pos,w;
node(int x,int y)
{
pos=x;
w=y;
}
bool friend operator < (node x,node y)
{
return x.w>y.w;
}
};
int head[],dis[],vis[],cnt;
void add(int u,int v,int w)
{
Edge[cnt].u=u;
Edge[cnt].v=v;
Edge[cnt].w=w;
Edge[cnt].next=head[u];
head[u]=cnt++;
}
void Dijkstra(int s)
{
dis[s]=;
priority_queue<node>q;
q.push(node(s,));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.pos])continue;
vis[now.pos]=; for(int i=head[now.pos];i!=-;i=Edge[i].next)
{
if(dis[now.pos]+Edge[i].w<dis[Edge[i].v])
{
dis[Edge[i].v]= dis[now.pos]+Edge[i].w;
q.push(node(Edge[i].v,dis[Edge[i].v]));
}
}
}
return ;
}
bool ok(int i, int j)
{
int len=strlen(str[i]);
if(str[i][len-] == str[j][] && str[i][len - ] == str[j][] && str[i][len-] == str[j][] && str[i][len-] == str[j][]) {
return true;
}
return false;
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
if(n==)
{
break;
}
cnt=;
memset(head,-,sizeof(head));
memset(dis,Inf,sizeof(dis));
memset(vis,,sizeof(vis));
for(int t=;t<=n;t++)
{
scanf("%d %s",&w[t],str[t]);
}
for(int t=;t<=n;t++)
{
for(int j=;j<=n;j++)
{ if(ok(t,j))
add(t,j,w[t]);
}
}
Dijkstra();
if(dis[n]!=Inf)
printf("%d\n",dis[n]);
else
{
printf("-1\n");
}
}
return ;
}

Idiomatic Phrases Game(最短路+注意坑点)的更多相关文章

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

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

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

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

  3. ZOJ2750_Idiomatic Phrases Game(最短路)

    Idiomatic Phrases Game Time Limit: 2 Seconds      Memory Limit: 65536 KB Tom is playing a game calle ...

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

  5. HDU 1546 Idiomatic Phrases Game 求助!help!!!

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

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

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

  7. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...

  8. ZOJ 2750 Idiomatic Phrases Game(Dijkstra)

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

  9. zoj 2750 Idiomatic Phrases Game

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

随机推荐

  1. Semantic Monocular SLAM for Highly Dynamic Environments面向高动态环境的语义单目SLAM

    一.摘要 当前单目SLAM系统能够实时稳定地在静态环境中运行,但是由于缺乏明显的动态异常处理能力,在动态场景变化与运动中往往会失败.作者为解决高度动态环境中的问题,提出一种语义单目SLAM架构,结合基 ...

  2. 【转载】requests库的7个主要方法、13个关键字参数以及响应对象的5种属性

    Python爬虫常用模块:requests库的7个主要方法.13个关键字参数以及响应对象的5种属性 原文链接: https://zhuanlan.zhihu.com/p/67489739

  3. 用 Python 写出这样的进度条,刷新了我对进度条的认知

    ❞ 1 简介 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给 ...

  4. JS学习第八天

    DOM访问列表框.下拉菜单的常用属性: form返回列表框.下拉菜单所在的表单对象; length返回列表框.下拉菜单的选项个数; options返回列表框.下拉菜单里所有选项组成的数组; defau ...

  5. 静态代理和动态代理(jdk/cglib)详解

    1.静态代理模式 代理模式上,基本上有Subject角色,RealSubject角色,Proxy角色.其中:Subject角色负责定义RealSubject和Proxy角色应该实现的接口:RealSu ...

  6. 如何将返回的JSon字符串用MAP格式读取

    语法是这样: ObjectMapper mapper = new ObjectMapper(); Map resultMap=null; resultMap = mapper.readValue(in ...

  7. clients-producer-网络处理与请求响应对接部分

  8. C#,js和sql实用技巧选2

    1.为什么"foo".Equals()不是好做法?因为当正确的写法是"foo".Equals(obj.value),却写成了"foo".Eq ...

  9. 快速排序&&归并排序

    快速排序 快速排序采用的是分治的策略,算法的具体实现过程是 1.确定一个数X(一般是选中间值X=q[l+r>>1]) 2.利用指针i,j,将数组中比X小的数放在一边,比X大的数放在另一边 ...

  10. 面试题-JavaSE语法

    1.Java 有没有 goto 语句? goto 是 Java 中的保留字,在目前版本的 Java 中没有使用.根据 James Gosling(Java 之父)编写的<The Java  Pr ...