hdu 1546 Idiomatic Phrases Game
http://acm.hdu.edu.cn/showproblem.php?pid=1546
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int a[maxn],n;
int g[maxn][maxn];
char str[maxn][maxn];
int dis[maxn];
bool vis[maxn]; void spfa()
{
queue<int>q;
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=inf;
dis[]=;
vis[]=true;
q.push();
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=false;
for(int i=; i<n; i++)
{
if(g[u][i]!=inf&&dis[i]>dis[u]+g[u][i])
{
dis[i]=dis[u]+g[u][i];
if(!vis[i])
{
q.push(i);
vis[i]=true;
}
}
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
for(int i=; i<n; i++)
{
cin>>a[i]>>str[i];
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(i==j) continue;
bool flag=false;
int k=strlen(str[i]);
for(int c=; c<; c++)
{
if(str[i][k+c-]!=str[j][c])
{
flag=true;
break;
}
}
if(!flag)
{
g[i][j]=a[i];
}
}
}
/*for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
printf("%d ",g[i][j]);
}
printf("\n");
}*/
spfa();
if(dis[n-]==inf) printf("-1\n");
else printf("%d\n",dis[n-]);
}
return ;
}
hdu 1546 Idiomatic Phrases Game的更多相关文章
- 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 //题目大意:现要进行单词 ...
- 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(dijkstra)
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Idiomatic Phrases Game(图论最短路)
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Idiomatic Phrases Game(最短路+注意坑点)
Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...
- ZOJ 2750 Idiomatic Phrases Game(Dijkstra)
点我看题目 题意 : 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的 ...
- zoj 2750 Idiomatic Phrases Game
迪杰斯特拉单源最短路算法.对成语进行预处理.做出邻接矩阵即可. #include<cstdio> #include<cstring> #include<cmath> ...
- ZOJ-2750 Idiomatic Phrases Game---Dijk最短路
题目链接: https://vjudge.net/problem/ZOJ-2750 题目大意: 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程 ...
随机推荐
- 关于栈和堆的定量分析(★firecat推荐★)
文章来源:http://blog.csdn.net/bigbug_zju/article/details/39525281 计算机系统中的堆和栈是跟程序员最密切的两个概念.如果没有栈和堆的概念,下面程 ...
- QTabWidget添加自定义样式
一.参考文章:http://bbs.csdn.net/topics/390632657?page=1 setStyleSheet("QTabWidget::pane{border-width ...
- Raid1源代码分析--读流程
这篇博文不足之处较多,重新整理了一下,链接:http://www.cnblogs.com/fangpei/p/3890873.html 我阅读的代码的linux内核版本是2.6.32.61.刚进实验室 ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- [原创作品]手把手教你怎么写jQuery插件
这次随笔,向大家介绍如何编写jQuery插件.啰嗦一下,很希望各位IT界的‘攻城狮’们能和大家一起分享,一起成长.点击左边我头像下边的“加入qq群”,一起分享,一起交流,当然,可以一起吹水.哈,不废话 ...
- 【转载】CocoaPods安装和使用教程
转自:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaP ...
- .NET基础拾遗(3)字符串、集合和流2
二.常用集合和泛型 2.1 int[]是值类型还是引用类型? .NET中无论是存储值类型对象的数组还是存储引用类型的数组,其本身都是引用类型,其内存也都是分配在堆上的.所有的数组类型都继承自Syste ...
- Eclipse MAT: Understand Incoming and Outgoing References
引用:http://xmlandmore.blogspot.hk/2014/01/eclipse-mat-understand-incoming-and.html?utm_source=tuicool ...
- C# 温故而知新:Stream篇(—)
C# 温故而知新:Stream篇(—) 目录: 什么是Stream? 什么是字节序列? Stream的构造函数 Stream的重要属性及方法 Stream的示例 Stream异步读写 Stream 和 ...
- css 文本域textarea显示成label标签
<html> <head> <title>textarea显示为label</title> <style type="text/ ...