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. Swift Alamofire

    转载:https://www.jianshu.com/p/07b1ec36a689最近AFNetworking的作者Matt Thompson 提出了一个新的类似AFNetworking的网络基础库, ...

  2. Android SQLite轻量级数据库的删除和查找操作

    今天主要是补充昨天的内容,本打算合成一章的,但是毕竟一天一天的内容写习惯了. 就这样继续昨天的,昨天只讲了创建以及增加和查询, 其实用法都差不多,今天学长也是在原有的基础上写的,还顺便融合了Share ...

  3. DCGAN实现

    DCGAN实现 代码 dcgan.py #!/usr/bin/env python # -*- coding: utf-8 -*- import os import math import argpa ...

  4. NMS系列

    NMS soft NMS softer NMS https://www.cnblogs.com/VincentLee/p/12579756.html

  5. XCTF-WEB-高手进阶区-NaNNaNaNNaN-Batman-笔记

    上来直接百度先搜下Batman -_-|| 不存在的传令兵么 本身是下载下来了一个文件web100 打开发现是如下内容 可以看出这个是个脚本语言,因此尝试修改后缀为html,发现是一个OK框 现在是想 ...

  6. [学习笔记] Numpy基础 系统学习

    [学习笔记] Numpy基础 上专业选修<数据分析程序设计>课程,老师串讲了Numpy基础,边听边用jupyter敲了下--理解+笔记. 老师讲的很全很系统,有些点没有记录,在PPT里就不 ...

  7. Vue 大量data及rules的data选项结构组织

    如果Vue文件需要很多的data成员及表单验证,建议使用类似结构 export default{ data(){ const model = { username: 'suzhen', passwor ...

  8. python设计模式之建造者模式

    python设计模式之建造者模式 ​ 建造者模式的适用范围:想要创建一个由多个部分组成的对象,而且它的构成需要一步接一步的完成.只有当各个部分都完成了,这个对象才完整.建造者模式表现为复杂对象的创建与 ...

  9. centos7上借助于xargs快速查询并卸载rpm软件

    在centos上卸载某些软件的时候,如果查询的软件包比较多,可以考虑使用xargs,边查询边卸载 如:下面在查询mysql包时候,将查询结果通过管道传送给xargs,然后使用rpm -e --node ...

  10. IDEA编写Scala代码时自动显示变量类型

    设置方法如下:settins -->Editor--> Code Style --> scala --Type Annotations  勾选框选部分 测试效果