题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1837

描述:

Isenbaev是国外的一个大牛。

现在有许多人要参加ACM ICPC。

一共有n个组,每组3个人。同组的3个人都是队友。

大家都想知道自己与大牛的最小距离是多少。

大牛与自己的最小距离当然是0。大牛的队友和大牛的最小距离是1。大牛的队友的队友和大牛的最小距离是2……以此类推。

如果实在和大牛没有关系的只好输出undefined了。

第一行读入n。表示有n个组。1 ≤ n ≤ 100

接下来n行,每行有3个名字,名字之间用空格隔开。每个名字的开头都是大写的。

每行输出一个名字,名字后面空格后输出数字a或者字符串undefined,a代表最小距离。

名字按字典序输出。

思路:

这一题我做的相当纠结,整了好几个小时。其实思路很简单,就是从Isenbaev这个人开始bfs,由于bfs可以记录每个点到起始搜索点的最短距离,正好就是答案。但是要注意没有Ixenbaev的情况,我已开始就栽在这了。

但是有几个问题:1:建图问题。每个点不是 int了,都是string类型,不好建图。后来想了想就用 了stl的map,把每个人名和int型映射,int就代表这个节点。先把所有人名插入到map里面,(这时已经按照字典序排列了,map容器的特性)。这样就建好图了。每个人名都有个int编号了,就用邻接矩阵建图再bfs就好了。bfs记录每个节点的dis[] 是多少,可以看看算法导论上这一节讲的挺好的。

2特殊case。有些情况是这组人没有一个人是 Isenbaev,这时候所有人都是undefined.一开始就没考虑到结果runtime error #3.

//g++ 4.7.2 0.031s
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <string> //Don't forget this,or failed compile by vc++
using namespace std;
const int M = 300;
bool adj[M][M], vis[M]; //adj[][]邻接矩阵
int n, dis[M];
void bfs(int start)
{
int u;
queue<int> q;
u = start;
dis[u] = 0;
q.push(u);
vis[u] = 1;
while (!q.empty())
{
u = q.front(); q.pop();
for (int v = 1; v <= n; ++v)
{
if (!vis[v] && adj[u][v])
{
vis[v] = 1;
dis[v] = dis[u] + 1; //记录距离
q.push(v);
}
}
}
}
int main()
{
int casenum;
scanf("%d", &casenum);
string name[M][4];
std::map<string, int> t;
for (int i = 1; i <= casenum; ++i)
for (int j = 1; j <= 3; ++j)
{
cin >> name[i][j]; //先把输入存在name[][]里面
t.insert(make_pair(name[i][j], 0));
}
int num = 1;
std::map<string, int>::iterator it;
for (it = t.begin(); it != t.end(); ++it) //给按照字典序排好的人名映射编号作为图的结点
(*it).second = num++;
for (int i = 1; i <= casenum; ++i)
{
int it1 = (*t.find(name[i][1])).second, it2 = (*t.find(name[i][2])).second, //找到这三个点,建立邻接矩阵
it3 = (*t.find(name[i][3])).second;
adj[it1][it2] = adj[it2][it3] = adj[it1][it3] = 1;
adj[it2][it1] = adj[it3][it2] = adj[it3][it1] = 1; //无向图,一开始忘了这一句害我调试半天
}
n = t.size(); //有多少个不同的人
it = t.find("Isenbaev");
if (it == t.end()) //处理没有Isenbaev的特殊情况
{
for (it = t.begin(); it != t.end(); ++it)
cout << (*it).first << " " << "undefined" << endl;
return 0;
}
bfs((*it).second);
for (it = t.begin(); it != t.end(); ++it)
{
if (dis[(*it).second] == 0 && (*it).first != "Isenbaev") //处理不连通的点,此时它的dis是0,但不是Isenbaev
cout << (*it).first << " " << "undefined" << endl;
else
cout << (*it).first << " " << dis[(*it).second] << endl;
}
return 0;
}

ural 1837. Isenbaev's Number bfs的更多相关文章

  1. ural 1837 Isenbaev's Number

    http://acm.timus.ru/problem.aspx?space=1&num=1837 #include <cstdio> #include <cstring&g ...

  2. URAL 1837. Isenbaev&#39;s Number (map + Dijkstra || BFS)

    1837. Isenbaev's Number Time limit: 0.5 second Memory limit: 64 MB Vladislav Isenbaev is a two-time ...

  3. 1837. Isenbaev's Number(floyd)

    1837 被数据结构部分打击的不行了 换地 刷点简单的 图论第一题 floyd水过 #include <iostream> #include<cstdio> #include& ...

  4. URAL 1008 - Image Encoding(bfs坑爹题)

    坑爹题,两种输入输出互相交换,裸bfs #include <stdio.h> #include <string.h> typedef struct { int x; int y ...

  5. 17. Letter Combinations of a Phone Number(bfs)

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  6. 第三次组队赛 (DFS&BFS)

    网站:CSUST 8月1日 先总结下,不得不说死的很惨,又是第三就不说了,一共7道题,AC了5道,但是有一个组三个人是做的个人赛,有两人AK了.......Orz,然后深搜还是大问题,宽搜倒是不急了. ...

  7. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  8. [LeetCode] Letter Combinations of a Phone Number(bfs)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. Nearest number - 2_暴力&&bfs

    Description Input is the matrix A of N by N non-negative integers. A distance between two elements A ...

随机推荐

  1. 为什么出现Wide character in print at a14.pl line 41

    [root@wx03 ~]# cat a14.pl use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; u ...

  2. 基于visual Studio2013解决C语言竞赛题之1042字符串比较

          题目 解决代码及点评 /********************************************************************** ...

  3. poj1830

    高斯消元求秩,难在构造方程. ; ; i < equ; i++)     {         ; j < var + ; j++)         {             cout & ...

  4. uva 620 Cellular Structure

    题目连接:620 - Cellular Structure 题目大意:给出一个细胞群, 判断该细胞的可能是由哪一种生长方式的到的, 输出该生长方式的最后一种生长种类, "SIMPLE&quo ...

  5. Linux下搭建Hadoop具体步骤

    装好虚拟机+Linux.而且主机网络和虚拟机网络互通. 以及Linux上装好JDK 1:在Linux下输入命令vi /etc/profile 加入HADOOP_HOME export JAVA_HOM ...

  6. How to search a table in a store proc and open the store proc

    1. select*fromdba_dependencieswherereferenced_name='CNTL_ISSUE'andTYPE='PROCEDURE' 2. selecttextfrom ...

  7. android第一天-------环境搭建

    今天正式第一天学习android的. 1.昨晚下班后回家跟同事刘江龙打了四把dota.还好,都赢了把对面虐成狗了.大多都是1300到1450的局,玩的很爽. 2.dota打完后给在湖南常德的女朋友打了 ...

  8. 来自中油瑞飞的SQL笔试题20131202

    1.有三张表,用户表,用户角色表,角色表, 使用sql显示如下内容: 用户ID,用户名,超级管理员,录入员,会计 也就是角色用逗号分隔. 解: 1.填充数据到表User select * from [ ...

  9. [置顶] 用mootools实现checkbox全选功能

    全选时,所有的单个checkbox都要选中,反过来也可以实现 //全选按钮 $('chkall').addEvent('click',function(){ $$('input[name=" ...

  10. cocos2dx+lua编译Android项目

    一.简单介绍 cocos2dx版本号:3.2 二.问题及解决方式 1.为项目开启Native支持,把项目转为C++项目. 1>.项目开启C++ Native支持,操作例如以下图 watermar ...