hdu 4460spfa用map来实现
#include<stdio.h>
#include<string.h>
#include <iostream>
#include <algorithm>
#include<string>//必须加这个
#include<cstdio>
#define N 11000
#define NN 1100
#define inf 999999999
#include<map>
#include<queue>
using namespace std;
map<string,int>ymap;//对于用这个
struct node {
int x,next;
}f[N*2];
int dis[NN],len,n,head[NN];
void Add(int a,int b) {
f[len].x=b;f[len].next=head[a];
head[a]=len++;
}
int spfa(int u) {
int visit[NN];
memset(visit,0,sizeof(visit));
int cur,i,max;
for(i=0;i<n;i++)
dis[i]=inf;
queue<int>q;
q.push(u);
dis[u]=0;
visit[u]=1;
while(!q.empty()) {
cur=q.front();
q.pop();
for(i=head[cur];i!=-1;i=f[i].next) {
u=f[i].x;
if(!visit[u]) {
q.push(u);
visit[u]=1;
dis[u]=dis[cur]+1;
}
}
}
max=-1;
for(i=0;i<n;i++)
if(dis[i]>max)
max=dis[i];
return max;
}
int main() {
int m,i,f1,f2,ans,cnt;
char s[20],s1[20];
while(scanf("%d",&n),n) {
ymap.clear();
for(i=0;i<n;i++) {
scanf("%s",s);
ymap[s]=i;
}
len=0;
scanf("%d",&m);
memset(head,-1,sizeof(head));
while(m--) {
scanf("%s%s",s,s1);
f1=ymap[s];
f2=ymap[s1];
Add(f1,f2);Add(f2,f1);
}
cnt=-1;
for(i=0;i<n;i++) {
ans=spfa(i);
cnt=cnt>ans?cnt:ans;
}
if(cnt>=inf)
printf("-1\n");
else
printf("%d\n",cnt);
}
return 0;
}
hdu 4460spfa用map来实现的更多相关文章
- hdu 1800 (map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...
- [HDU 4585] Shaolin (map应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...
- hdu 统计难题(map)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 map的强大之处,但是运行时间太长. 代码: #include <stdio.h> ...
- ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...
- HDU 2112 HDU Today Dij+map转化
HDU Today 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并 ...
- hdu 4585 map **
题意: Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every ...
- HDU 4585 Shaolin(map应用+二分)
题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进 ...
- K-D树问题 HDU 4347
K-D树可以看看这个博客写的真心不错!这里存个版 http://blog.csdn.net/zhjchengfeng5/article/details/7855241 HDU 4349 #includ ...
- 第一周训练 | STL和基本数据结构
A - 圆桌问题: HDU - 4841 #include<iostream> #include<vector> #include<stdio.h> #includ ...
随机推荐
- memcache的分布式配置
public static class MemcacheHelper { private static MemcachedClient mc; static MemcacheHelper() { St ...
- android studio 新建文件出错
- Design Compiler 综合
综合(synthesis) = 转换(translation) + 优化(logic optimization) + 映射(gate mapping): 转换阶段将HDL语言描述的电路用门级逻辑实现. ...
- virtualbox没有64位选项
今天安装的virtualbox想安装一下sql server 测试一下 在安装系统的时候发现没有64位系统的选项,在网上找了一下 发现是 在BIOS里面有一个选项没有开启, 是 Intel virt ...
- phpstorm 格式化代码
MAC 安装phpcs.phpcbf composer global require 'squizlabs/php_codesniffer=*' Changed current directory t ...
- 洛谷 P1507 NASA的食物计划
题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安 全技术问题一直大伤脑筋,因此在各方压力下终止了航天 飞机的历史,但是此类事情会不会在以后发生,谁也无法 保证,在遇到这类航天问题时,解 ...
- iOS猜拳游戏源码
利用核心动画和Quartz2D做的一个小游戏.逻辑十分简单. 源码下载:http://code.662p.com/<ignore_js_op> 详细说明:http://ios.662p.c ...
- k8s 重要概念[转]
在实践之前,必须先学习 Kubernetes 的几个重要概念,它们是组成 Kubernetes 集群的基石. Cluster Cluster 是计算.存储和网络资源的集合,Kubernetes 利用这 ...
- .net MVC下跨域Ajax请求(JSONP)
一.JSONP(JSON with Padding) 客户端: <script type="text/javascript"> function TestJsonp() ...
- Vue.js系列之vue-router(上) (转载自向朔1992)
概述 Vue非常适用于实践单页面应用程序也就是平时大家说的比较多的SPA(single page application),这点应该了解过Vue的应该都知道吧.一般的单页面应用是基于路由或页面之间的链 ...