1837. Isenbaev's Number

Time limit: 0.5 second

Memory limit: 64 MB
Vladislav Isenbaev is a two-time champion of Ural, vice champion of TopCoder Open 2009, and absolute champion of ACM ICPC 2009. In the time you will spend reading this problem statement Vladislav would
have solved a problem. Maybe, even two…
Since Vladislav Isenbaev graduated from the Specialized Educational and Scientific Center at Ural State University, many of the former and present contestants at USU have known him for quite a few years.
Some of them are proud to say that they either played in the same team with him or played in the same team with one of his teammates…
Let us define Isenbaev's number as follows. This number for Vladislav himself is 0. For people who played in the same team with him, the number is 1. For people who weren't his teammates but
played in the same team with one or more of his teammates, the number is 2, and so on. Your task is to automate the process of calculating Isenbaev's numbers so that each contestant at USU would know their proximity to the ACM ICPC champion.

Input

The first line contains the number of teams n (1 ≤ n ≤ 100). In each of the following n lines you are given the names of the three members of the corresponding team. The names
are separated with a space. Each name is a nonempty line consisting of English letters, and its length is at most 20 symbols. The first letter of a name is capital and the other letters are lowercase.

Output

For each contestant mentioned in the input data output a line with their name and Isenbaev's number. If the number is undefined, output “undefined” instead of it. The contestants must be ordered lexicographically.

Sample

input output
7
Isenbaev Oparin Toropov
Ayzenshteyn Oparin Samsonov
Ayzenshteyn Chevdar Samsonov
Fominykh Isenbaev Oparin
Dublennykh Fominykh Ivankov
Burmistrov Dublennykh Kurpilyanskiy
Cormen Leiserson Rivest
Ayzenshteyn 2
Burmistrov 3
Chevdar 3
Cormen undefined
Dublennykh 2
Fominykh 1
Isenbaev 0
Ivankov 2
Kurpilyanskiy 3
Leiserson undefined
Oparin 1
Rivest undefined
Samsonov 2
Toropov 1

题意:给出n个3人小组,Isenbaev被编号为0。他的队友编号为1。他队友的队友被编号为2。。

。以此类推。假设没有办法通过关系联系到Isenbaev。则输出“undefined”。其它的输出编号。

解析:先将全部的人用map映射出一个编号,这里就利用了map能够自己主动按字典序排序的特点,把全部出现过的字符串直接放到map里。遍历的时候就是有字典序的了。然后就是以编号为顶点建无向图了,各组员之间的距离为1,这样用Dijkstra就能够了求解最短距离了。当然BFS也能够搜出最短路径长度。

AC代码:

#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
#define INF 1e7
const int maxn = 302; int g[maxn][maxn], d[maxn];
string a[maxn][3];
map<string, int> m;
bool used[maxn]; void dijkstra(int s, int V){ //Dijkstra算法
fill(d, d + V, INF);
fill(used, used + V, false);
d[s] = 0; while(true){
int v = -1;
for(int u=0; u<V; u++){
if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
} if(v == -1) break;
used[v] = true; for(int u=0; u<V; u++){
d[u] = min(d[u], d[v] + g[v][u]);
}
}
} int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif //sxk int n;
while(scanf("%d", &n)==1){
for(int i=0; i<n; i++){
cin >> a[i][0] >> a[i][1] >> a[i][2];
m[ a[i][0] ] = 0; m[ a[i][1] ] = 0; m[ a[i][2] ] = 0; //把字符串放到map里
} int num = 0;
map<string, int>::iterator it;
for(it = m.begin(); it!=m.end(); it++){
it->second = ++num; //给个字符串编号
} for(int i=0; i<maxn; i++)
for(int j=0; j<maxn; j++) g[i][j] = INF;
for(int i=0; i<n; i++){ //初始化组员之间距离
int f1 = m.find(a[i][0])->second, f2 = m.find(a[i][1])->second, f3 = m.find(a[i][2])->second;
g[f1][f2] = g[f2][f3] = g[f1][f3] = 1;
g[f2][f1] = g[f3][f2] = g[f3][f1] = 1;
} int len = m.size();
it = m.find("Isenbaev");
if(it == m.end()){
for(it=m.begin(); it!=m.end(); it++) cout<<it->first<<" "<<"undefined"<<endl;
continue;
} dijkstra(it->second, len + 1); for(it=m.begin(); it!=m.end(); it++){
cout<<it->first<<" ";
if(d[it->second] == INF) puts("undefined");
else cout<<d[it->second]<<endl;
}
}
return 0;
}

URAL 1837. Isenbaev&#39;s Number (map + Dijkstra || BFS)的更多相关文章

  1. 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

  2. Find the duplicate Number (鸽巢原理) leetcode java

    问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  3. c++的关联容器入门(map and set)

    目录 std::map std::set C++的关联容器主要是两大类map和set 我们知道谈到C++容器时,我们会说到 顺序容器(Sequence containers),关联容器(Associa ...

  4. number(4,2)

     number(4,2)  ##.## 例如:45.23 number(6,2)就是####.##   例如:9994.11   4代表总共有效位数为4位2代表小数位为2位

  5. &lt;LeetCode OJ&gt; 26 / 264 / 313 Ugly Number (I / II / III)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  6. HDU2665Kth number (主席树+离散)

    Give you a sequence and ask you the kth big number of a inteval. InputThe first line is the number o ...

  7. 2019浙大校赛--J--Extended Twin Composite Number(毒瘤水题)

    毒瘤出题人,坑了我们好久,从基本的素数筛选,到埃氏筛法,到随机数快速素数判定,到费马小定理,好好的水题做成了数论题. 结果答案是 2*n=n+3*n,特判1,2. 以下为毒瘤题目: 题目大意: 输入一 ...

  8. 白话空间统计之:Moran&#39;s I(莫兰指数)

    前两天聊了空间统计学里面的两个经典概念,今天来说说第一篇文章留下的大坑:Moran's I. 首先,Moran's I这个东西.官方叫做:莫兰指数,是澳大利亚统计学家帕特里克·阿尔弗雷德·皮尔斯·莫兰 ...

  9. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

随机推荐

  1. LeetCode(15): 每k个一组翻转链表

    hard! 题目描述: 给出一个链表,每 k 个节点为一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. ...

  2. java 多态缺陷

    一,会覆盖私有方法 package object; class Derive extends Polymorphism{ public void f1() { System.out.println(& ...

  3. SOA 解惑

    SOA 解惑 SOA 不是一种技术,它是一种设计方法.最近一段时间我碰到了很多关于 SOA 的具有误导性的文章.尤其是,有些人混淆了 SOA 和诸如 BPM.ESB 以及复合事件处理 (CEP) 之类 ...

  4. Ocelot + IdentityServer4 构建 GateWay

    上一篇已经构建好了例子,接下来将IdentityServer4添加到Ocelot中去实现 配置一个客户端配置,可以构建一个简单的客户端信息,这里我用的混合模式,配置比较多,对于客户端模式而言实际很多都 ...

  5. centos7 mysql5.7 rpm 安装

    卸载MariaDB CentOS7默认安装MariaDB而不是MySQL,而且yum服务器上也移除了MySQL相关的软件包.因为MariaDB和MySQL可能会冲突,故先卸载MariaDB. 查看已安 ...

  6. Sqoop的安装及简单使用

    SQOOP是用于对数据进行导入导出的. (1)把MySQL.Oracle等数据库中的数据导入到HDFS.Hive.HBase中   (2)把HDFS.Hive.HBase中的数据导出到MySQL.Or ...

  7. Windows下安装Tensorflow(python3.6):记录过程

    安装前的情况: 之前使用的都是python2.7,但是tensorflow不支持2.x版本,那只有基于在3.x版本进行安装了 前段时间,我安装VS2017的时候安装了python3.6于是想在此基础上 ...

  8. win10下 Jupyter Notebook不运行python 3怎么办?

    Jupyter Notebook不运行python 3怎么办? 内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用 我已经安装了Python 2的Anaco ...

  9. oracle数据库连接错误解决办法

    ORA-28547 连接服务器失败,可能是Oracle Net 管理错误 原文地址:http://www.linuxidc.com/Linux/2014-11/109686.htm 上周去给客户培训O ...

  10. jstat命令总结

    jvm统计信息监控工具 一. jstat是什么 jstat是JDK自带的一个轻量级小工具.全称"Java Virtual Machine statistics monitoring tool ...