博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Idiomatic Phrases Game(最短路+注意坑点)
阅读量:5009 次
发布时间:2019-06-12

本文共 3293 字,大约阅读时间需要 10 分钟。

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

55 12345978ABCD23415 23415608ACBD34127 34125678AEFD412315 23415673ACC341234 41235673FBCD2156220 12345678ABCD30 DCBF5432167D0

Sample Output

17-1 这个题的坑点在字符串的长度上 代码: vector版
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x3f3f3f3fconst int maxn=1e5+5;typedef long long ll;using namespace std;char str[1005][105];int w[1005];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
vec[1005];int dis[1005];int vis[1005];int n,m;void init(){ for(int t=1;t<=n;t++) { dis[t]=Inf; } memset(vis,0,sizeof(vis));}void Dijkstra(int s){ priority_queue
q; q.push(node(s,0)); dis[s]=0; while(!q.empty()) { node now=q.top(); q.pop(); if(vis[now.pos])continue; vis[now.pos]=1; for(int t=0;t

邻接表版

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x3f3f3f3fconst int maxn=1e5+5;typedef long long ll;using namespace std;struct edge{ int u,v,w; int next;}Edge[10*maxn];int w[1005];char str[1005][105];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[1005],dis[1005],vis[1005],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]=0; priority_queue
q; q.push(node(s,0)); while(!q.empty()) { node now=q.top(); q.pop(); if(vis[now.pos])continue; vis[now.pos]=1; for(int i=head[now.pos];i!=-1;i=Edge[i].next) { if(dis[now.pos]+Edge[i].w

 

转载于:https://www.cnblogs.com/Staceyacm/p/11274685.html

你可能感兴趣的文章
条形码扫描枪数据读取的问题
查看>>
$this->autoRender = false
查看>>
健壮的 Java 基准测试
查看>>
phpstorm查看类的继承关系
查看>>
git create clone(仓库)
查看>>
chmod修改文件权限的命令
查看>>
新博客牵至简书
查看>>
矩阵求逆
查看>>
在 Windows 8、Windows 10 桌面模式下的 .NET Framework 程序中,引用 Windows.Runtime 的 API。...
查看>>
2015 8月24号 工作计划与实行
查看>>
MVC AJAX
查看>>
Google Map API V3开发(6) 代码
查看>>
Kafka初入门简单配置与使用
查看>>
第三章Git使用入门
查看>>
Amd,Cmd, Commonjs, ES6 import/export的异同点
查看>>
cocos2dx-Lua与Java通讯机制
查看>>
上下文管理器之__enter__和__exit__
查看>>
android3.2以上切屏禁止onCreate()
查看>>
winform文件迁移工具
查看>>
delphi DCC32命令行方式编译delphi工程源码
查看>>