迪杰斯特拉单源最短路算法。对成语进行预处理。做出邻接矩阵即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ;
int c[maxn], len[maxn], cost[maxn][maxn], flag[maxn], e[maxn];
char s[maxn][];
int main()
{
int n, i, j, ii;
while (~scanf("%d", &n))
{
if (n == ) break;
for (i = ; i < n; i++) scanf("%d%s", &c[i], s[i]);
for (i = ; i < n; i++) len[i] = strlen(s[i]);
memset(flag, , sizeof(flag));
for (i = ; i <= n; i++) for (j = ; j <= n; j++) cost[i][j] = ;
for (i = ; i < n; i++)
{
for (j = ; j < n; j++)
{
if (i != j&&s[j][] == s[i][len[i] - ] && s[j][] == s[i][len[i] - ] && s[j][] == s[i][len[i] - ] && s[j][] == s[i][len[i] - ])
cost[i][j] = c[i];
}
}
for (i = ; i < n; i++) e[i] = cost[][i];
e[] = ; flag[] = ; int x, uu;
for (ii = ; ii < n - ; ii++)
{
int minn = ; uu = ;
for (i = ; i < n; i++)
{
if (!flag[i] && e[i] < minn)
{
x = i;
minn = e[i];
uu = ;
}
}
if (!uu) continue;
flag[x] = ;
for (i = ; i < n; i++)
if (!flag[i] && cost[x][i] != && e[x] + cost[x][i] < e[i])
e[i] = e[x] + cost[x][i];
}
if (e[n - ] != ) printf("%d\n", e[n - ]);
else printf("-1\n");
}
return ;
}

zoj 2750 Idiomatic Phrases Game的更多相关文章

  1. 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 ...

  2. ZOJ 2750 Idiomatic Phrases Game(Dijkstra)

    点我看题目 题意 : 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的 ...

  3. Idiomatic Phrases Game(图论最短路)

    Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. HDU 1546 Idiomatic Phrases Game 求助!help!!!

    Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. Idiomatic Phrases Game(最短路+注意坑点)

    Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...

  6. HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)

    题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...

  7. hdu 1546 Idiomatic Phrases Game

    http://acm.hdu.edu.cn/showproblem.php?pid=1546 #include <cstdio> #include <iostream> #in ...

  8. ZOJ-2750 Idiomatic Phrases Game---Dijk最短路

    题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...

  9. 北大ACM - POJ试题分类

    1.入门水题 可用于练手与增强自信 POJ-1003POJ-1004 POJ-1005 POJ-1207 POJ-3299 POJ-2159 POJ-1083POJ-3094 2.初级 2.1. 基本 ...

随机推荐

  1. Linux系统下给非root用户添加sudo权限

    Linux系统下给非root用户添加sudo权限 有时,在linux系统中非root用户运行sudo命令,会提示类似信息:  xxx is not in the sudoers file. This ...

  2. springboot学习笔记-1 第一个springboot示例

    springboot是一个微框架,其设计的目的是为了简化spring框架的搭建和配置过程.从而使开发人员不再需要定义样板化的配置.下面是springboot的入门案例:它演示了利用springboot ...

  3. oracle登录时shared memory realm does not exist的解决方法

    解决办法:1.用CMD进入命令行2.sqlplus /nolog 3.conn / as sysdba4.startup   然后用sqlplus进入命令  

  4. wpf 寻找某个控件下的子控件

    /// <summary> /// 寻找某个控件下的子控件 /// </summary> /// <typeparam name="ChildType" ...

  5. python3 数据类型

    Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) Number(数字) Py ...

  6. 加班计时App

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. 使用Nginx+Lua(OpenResty)开发高性能Web应用

    摘自(http://jinnianshilongnian.iteye.com/blog/2280928) 在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等 ...

  8. jmeter命令行运行-单节点

    jmeter有自己的GUI页面,但是当线程数很多或者现在有很多的测试场景都是基于linux下进行压测,这时我们可以使用jmeter的命令行方式来执行测试,该篇文章介绍jmeter单节点命令运行方式. ...

  9. 3、href和src的区别

    href:将现在的页面连接到新的页面 src:将需要引用的东西添加到现在的页面 <a href="mailto:youremailaddress@host.com">C ...

  10. NVIC

    1中断:每一个中断都会对应一个服务程序 2NVIC 他的做用是负责中断优先级管理 3IP bit[7:4]每一个中断都有一个IP寄存器,用于设置中断相关的属性 AIRCR[10:8]只一个AIRCR寄 ...