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. 【JZOJ4726】种花 题解(贪心+堆)

    题目大意:在一个长度为$n$的环型序列中取出$m$个数使这$m$个数的和最大,且要求这$m$个数互不相邻. ---------------------- 考虑维护$nxt$和$lst$,即一个数的前驱 ...

  2. 024_go语言中的缓冲通道

    代码演示 package main import "fmt" func main() { messages := make(chan string, 2) messages < ...

  3. c语言学习笔记之typedef

    这是我觉得这个博主总结的很好转载过来的 原地址:https://blog.csdn.net/weixin_41632560/article/details/80747640 C语言语法简单,但内涵却博 ...

  4. MySQL回表查询

    一.MySQL索引类型 1.普通索引:最基本的索引,没有任何限制 2.唯一索引(unique index):索引列的值必须唯一,但是允许为空 3.主键索引:特殊的唯一索引,但是不允许为空,一般在建表的 ...

  5. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

  6. Linux系统安装Nginx(Centos7)

    Nginx是一款轻量级的网页服务器.反向代理服务器.它最常的用途是提供反向代理服务,还可以做负载均衡.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.服务端很多场景都需要使用,这篇 ...

  7. 【工具-代码】OSS阿里云存储服务-代码实现

    上一章节[工具]OSS阿里云存储服务--超级简单--个人还是觉得Fastdfs好玩 https://www.cnblogs.com/Yangbuyi/p/13488323.html 接上一个文章讲解还 ...

  8. Uni-app从入门到实战

    前言 uni-app是一个使用vue.js开发跨平台应用的前端框架,开发者只需要编写一套代码,便可以发布到IOS.Android和微信小程序等多个平台.所以我打算学习下这个框架,快速浏览了一遍官网之后 ...

  9. 第三章 kubernetes核心原理

    kubernetes API Server 提供了Kubernetes各类资源对象(如pod,re,service等)的增删改查及watch等Http Rest接口,成为集群内各个功能模块之间数据交互 ...

  10. python3 输出中文、日文等等乱码问题的解决办法

    例如: url = 'https://zozo.jp/shop/mrolive/goods-sale/44057773/?did=73037089' resp = requests.get(url=u ...