题目

1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新。

2.看懂题目意思和案例的解法很重要。

#define  _CRT_SECURE_NO_WARNINGS
//题目大意:现要进行单词接龙,要求前一个单词的最后四个字符和后一个单词的前四个字符相同,
//给你一串单词,并给出由当前单词找到下一个单词所需花费的时间,
//然后求由第一个单词找到最后一个单词所需的最短时间 //思路:最短路,如果一个单词的最后四个字符和下一个单词的前四个字符相同,
//则在两个单词间连一条有向边,边的权值为第一个单词所需的花费时间,则转换为了图,
//求第一个单词到最后一个单词的最短路,Dijksta算法
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const int MAXN=; #define typec int
const typec INF=0x3f3f3f3f;//防止后面溢出,这个不能太大
bool vis[MAXN];
typec cost[MAXN][MAXN],a[MAXN];
typec lowcost[MAXN];
void Dijkstra(int n,int beg)
{
for(int i=;i<=n;i++)
{
lowcost[i]=cost[beg][i];vis[i]=false;
}
for(int i=;i<=n;i++)
{
typec temp=INF;
int k=-;
for(int j=;j<=n;j++)
if(!vis[j]&&lowcost[j]<temp)
{
temp=lowcost[j];
k=j;
}
if(k!=-){
vis[k]=true;
for(int l=;l<=n;l++)
if(!vis[l])
if(lowcost[l]>lowcost[k]+cost[k][l])
lowcost[l]=lowcost[k]+cost[k][l];
}
}
} int main()
{
int n,i,j,len;
char b[MAXN][],c[MAXN][],d[MAXN][];
while(scanf("%d",&n),n)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
cost[i][j]=(i==j)? :INF; for(i=;i<=n;i++)
{
scanf("%d%s",&a[i],b[i]);
len=strlen(b[i]);
c[i][]=b[i][],c[i][]=b[i][],c[i][]=b[i][],c[i][]=b[i][];c[i][]='\0';
d[i][]=b[i][len-],d[i][]=b[i][len-],d[i][]=b[i][len-],d[i][]=b[i][len-];d[i][]='\0';
for(j=;j<i;j++)
{
if(strcmp(d[i],c[j])==)
if(cost[i][j]>a[i])
cost[i][j]=a[i];
if(strcmp(d[j],c[i])==)
if(cost[j][i]>a[j])
cost[j][i]=a[j];
}
}
Dijkstra(n,);
if(lowcost[n]==INF)
printf("-1\n");
else
printf("%d\n",lowcost[n]);
}
return ;
}

HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)的更多相关文章

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

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

  2. hdu 1546 Idiomatic Phrases Game

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

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

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

  4. Hdu-2112 HDU Today (单源多点最短路——Dijsktra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给你N个公交车站,起点,终点,各站之间的距离,求起点到终点之间的最短距离.(起点终点相 ...

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

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

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

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

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

  8. ZOJ2750_Idiomatic Phrases Game(最短路)

    Idiomatic Phrases Game Time Limit: 2 Seconds      Memory Limit: 65536 KB Tom is playing a game calle ...

  9. hdu 1546(dijkstra)

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

随机推荐

  1. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  2. EF调用存储过程

    var objPar1 = new System.Data.Objects.ObjectParameter("ExeResults",typeof(bool)); var objP ...

  3. oracle11.0.2 64位版本 Toad连接

    今天重装了系统 oracle  oracle客户端 之前连不上toad 今天总结 客户端路径:E:\app\ruonanxiao-pc\product\11.2.0\client_1 服务端路径:E: ...

  4. 最初程序员的思维“修炼”之四——Android平台开发的“强制关闭”解决思路

    我和我的朋友参加一个比赛——物联网应用技能大赛,这个大赛所要求的技能有,硬件技术,Android平台开发技术,.NET平台开发技术,所以这是一个团队合作的比赛,基本上没有人能同时掌握这三种技术(在校生 ...

  5. C# 解析XML格式的字符串

    public CreateOrderReturnResult GetCreateOrderReturnApi() { var result = new CreateOrderReturnResult( ...

  6. jQuery 添加元素和删除元素

    jQuery - 添加元素 append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元 ...

  7. 初级jQuery的使用

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. Infobright高性能数据仓库

    1.  概述 Infobright是一款基于独特的专利知识网格技术的列式数据库.Infobright简单易用,快速安装部署,使用中无需复杂操作,能大幅度减少管理工作:在应对50TB甚至更多数据量进行多 ...

  9. Storm集群安装详解

    storm有两种操作模式: 本地模式和远程模式. 本地模式:你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来; 远端模式:你提交的topology会在一个集群的机器 ...

  10. oracle作业

    http://blog.csdn.net/hao_ds/article/details/38382931 oracle作业各种参数的详细介绍