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. 文档写作利器:Markdown

    大佬的文章,写的很好,里面推荐的Markdown编辑工具很不错,值的推荐. 文档写作利器:Markdown_网络_xcbeyond|疯狂源自梦想,技术成就辉煌-CSDN博客https://blog.c ...

  2. Linux入门-基本概念

    本文主要介绍linux基础概念介绍,对基本概念了解后,更好入门. 这里主要介绍一下几个概念 什么是linux GNU项目和自由软件基金会 linux发行版 什么是linux   也许大家都已经知道,L ...

  3. spring boot项目集成zuul网关

    1 zuul简介 Zuul 的官方介绍是 “Zuul is the front door for all requests from devices and web sites to the back ...

  4. 史上最简单操作!!!!!!!Window Server2012 修改远程桌面端口号

    Window Server2012 修改远程桌面端口号   Win + R 输入 regedit 打开注册表编辑器 在注册表编辑器中找到 PortNumber 双击 PortNumber,选择10进制 ...

  5. C++字符串转整形、浮点型stof()、atoi()、strtol()等

    头文件:#include<stdlib.h>string str;stof:float val=stof(str);atoi:int val=atoi(str);atol:long val ...

  6. 2020-06-02:千万级数据量的list找一个数据。

    福哥答案2020-06-02: 对于千万级长度的数组单值查找:序号小的,单线程占明显优势:序号大的,多线程占明显优势.单线程时间不稳定,多线程时间稳定. go语言测试代码如下: package mai ...

  7. C#LeetCode刷题之#39-组合总和(Combination Sum)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3663 访问. 给定一个无重复元素的数组 candi ...

  8. C#LeetCode刷题-Map

    Map篇 # 题名 刷题 通过率 难度 846 一手顺子   33.7% 中等 855 考场就座   20.8% 中等

  9. 轻量级Java EE企业应用实战:Struts2+Spring5+Hibernate5/JPA2

    轻量级Java EE企业应用实战(第5版)——Struts 2+Spring 5+Hibernate 5/JPA 2整合开发是<轻量级Java EE企业应用实战>的第5版,这一版保持了前几 ...

  10. python 03 常用操作符

    1. e记法,科学计数法. AeB   A,B为整数,A*10的B次方. 2. 逻辑运算,真为1,假为0,最好不使用这个计算 true(1)    false(0) true+true=2 3.类型转 ...