Idiomatic Phrases Game(最短路+注意坑点)
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(最短路+注意坑点)的更多相关文章
- ZOJ-2750 Idiomatic Phrases Game---Dijk最短路
题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...
- Idiomatic Phrases Game(图论最短路)
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- ZOJ2750_Idiomatic Phrases Game(最短路)
Idiomatic Phrases Game Time Limit: 2 Seconds Memory Limit: 65536 KB Tom is playing a game calle ...
- 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 ...
- HDU 1546 Idiomatic Phrases Game 求助!help!!!
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)
题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
- ZOJ 2750 Idiomatic Phrases Game(Dijkstra)
点我看题目 题意 : 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的 ...
- zoj 2750 Idiomatic Phrases Game
迪杰斯特拉单源最短路算法.对成语进行预处理.做出邻接矩阵即可. #include<cstdio> #include<cstring> #include<cmath> ...
随机推荐
- Requests接口测试库-官网快速上手
Requests 一个发送HTTP请求的库基于urllib3,相比自带的库,提供了更高效简洁的可用方法,测试从业者用来做接口测试的一个好工具 文章内容均来自官网:https://requests.re ...
- 面试:Java基础知识(一)
1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽 ...
- 云计算&存储测试:FIO工具入门与实战
一.关于FIO 1.1 简介 FIO是一个开源的I/O压力测试工具,主要是用来测试磁盘的IO性能,也可测试cpu,nic的IO性能.它可以支持13种不同的I/O引擎,包括:sync,mmap, lib ...
- C语言输出颜色
命令后界面输出颜色 嵌入式终端界面输出日志时,为了区分输出的有用信息.错误信息,可以给不同级别的输出加上不同的颜色,以方便查看. 下面是颜色的定义: //颜色宏定义 #define NONE &quo ...
- Android Studio连接数据库实现增删改查
源代码如下: DBUtil.java: package dao; import java.sql.Connection; import java.sql.DriverManager; import j ...
- SSH安全免密码登录:ssh key
ssh key 使用非对称加密方式生成公钥和私钥 私钥存放在本地~/.ssh目录 公钥可以对外公开,放在服务器的~/.ssh/authorized_keys 1.linux平台生成ssh key ss ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- Vue Vue-loader / VueLoaderPlugin / Webpack
在不用VueCli创建项目的时候,手写引入vue的时候,配置webpack的时候发现了这个问题 webpack vue-loader was used without the correspondin ...
- SpringBoot实现本地存储文件上传及提供HTTP访问服务
笔者计划为大家介绍分布式文件系统,用于存储应用的图片.word.excel.pdf等文件.在开始介绍分布式文件系统之前,为大家介绍一下使用本机存储来存放文件资源. 二者的核心实现过程是一样的: 上传文 ...
- Netty多协议开发
HTTP协议开发 post与get的区别 1)get用于信息获取,post用于更新资源. 2)get数据放在请求行中,post数据放在请求体内. 3)get对数据长度有限制(2083字节),post没 ...