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
  1. 7
  2. Isenbaev Oparin Toropov
  3. Ayzenshteyn Oparin Samsonov
  4. Ayzenshteyn Chevdar Samsonov
  5. Fominykh Isenbaev Oparin
  6. Dublennykh Fominykh Ivankov
  7. Burmistrov Dublennykh Kurpilyanskiy
  8. Cormen Leiserson Rivest
  1. Ayzenshteyn 2
  2. Burmistrov 3
  3. Chevdar 3
  4. Cormen undefined
  5. Dublennykh 2
  6. Fominykh 1
  7. Isenbaev 0
  8. Ivankov 2
  9. Kurpilyanskiy 3
  10. Leiserson undefined
  11. Oparin 1
  12. Rivest undefined
  13. Samsonov 2
  14. Toropov 1

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

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

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

AC代码:

  1. #include <cstdio>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cstring>
  6. #include <map>
  7. using namespace std;
  8. #define INF 1e7
  9. const int maxn = 302;
  10.  
  11. int g[maxn][maxn], d[maxn];
  12. string a[maxn][3];
  13. map<string, int> m;
  14. bool used[maxn];
  15.  
  16. void dijkstra(int s, int V){ //Dijkstra算法
  17. fill(d, d + V, INF);
  18. fill(used, used + V, false);
  19. d[s] = 0;
  20.  
  21. while(true){
  22. int v = -1;
  23. for(int u=0; u<V; u++){
  24. if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
  25. }
  26.  
  27. if(v == -1) break;
  28. used[v] = true;
  29.  
  30. for(int u=0; u<V; u++){
  31. d[u] = min(d[u], d[v] + g[v][u]);
  32. }
  33. }
  34. }
  35.  
  36. int main(){
  37. #ifdef sxk
  38. freopen("in.txt", "r", stdin);
  39. #endif //sxk
  40.  
  41. int n;
  42. while(scanf("%d", &n)==1){
  43. for(int i=0; i<n; i++){
  44. cin >> a[i][0] >> a[i][1] >> a[i][2];
  45. m[ a[i][0] ] = 0; m[ a[i][1] ] = 0; m[ a[i][2] ] = 0; //把字符串放到map里
  46. }
  47.  
  48. int num = 0;
  49. map<string, int>::iterator it;
  50. for(it = m.begin(); it!=m.end(); it++){
  51. it->second = ++num; //给个字符串编号
  52. }
  53.  
  54. for(int i=0; i<maxn; i++)
  55. for(int j=0; j<maxn; j++) g[i][j] = INF;
  56. for(int i=0; i<n; i++){ //初始化组员之间距离
  57. int f1 = m.find(a[i][0])->second, f2 = m.find(a[i][1])->second, f3 = m.find(a[i][2])->second;
  58. g[f1][f2] = g[f2][f3] = g[f1][f3] = 1;
  59. g[f2][f1] = g[f3][f2] = g[f3][f1] = 1;
  60. }
  61.  
  62. int len = m.size();
  63. it = m.find("Isenbaev");
  64. if(it == m.end()){
  65. for(it=m.begin(); it!=m.end(); it++) cout<<it->first<<" "<<"undefined"<<endl;
  66. continue;
  67. }
  68.  
  69. dijkstra(it->second, len + 1);
  70.  
  71. for(it=m.begin(); it!=m.end(); it++){
  72. cout<<it->first<<" ";
  73. if(d[it->second] == INF) puts("undefined");
  74. else cout<<d[it->second]<<endl;
  75. }
  76. }
  77. return 0;
  78. }

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. Java 把异常传递给控制台

    最简答而又不用写多少代码就能保护异常信息的方法,就是把它们从main()传递到控制台,对于简单的程序可以像这样: package exceptions; //: exceptions/MainExce ...

  2. Archlinux系统配置学习笔记(一)

    本文档是有关Archlinux系统配置的学习笔记,参考和学习的是Archlinux官方网站上的相应文档:General Recommendations. 这里的配置主要是针对按照官方网站上的文档刚刚完 ...

  3. Java编程的逻辑 (55) - 容器类总结

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  4. SQL Server 4

    一.视图 1.创建视图 1)选中数据库中的表中的视图处,右键选择新建视图,即: 2)在弹出“添加表”对话框中,单击“表”标签,选择要添加的表,点击添加,即: 3)选中要建立联系的列名的复选框,然后拖动 ...

  5. JAVAssist字节码操作

    Java动态性的两种常见实现方式 字节码操作 反射 运行时操作字节码可以让我们实现如下功能: 动态生成新的类 动态改变某个类的结构(添加/删除/修改  新的属性/方法) 优势: 比反射开销小,性能高 ...

  6. mybatis的快速入门

    说明: 在这个部分,会写个简单的入门案例. 然后,会重新写一个,更加严格的程序案例. 一:案例一 1.最终的目录结构 2.新建一个普通的Java项目,并新建lib 在项目名上右键,不是src. 3.导 ...

  7. 001 Ajax中XMLHttpRequest的讲解

    1.介绍 2.方法 3.程序位置设计 4.程序(针对XMLHttpRequest) <%@ page language="java" contentType="te ...

  8. [代码审计]云优cms V 1.1.2前台多处sql注入,任意文件删除修复绕过至getshell

    0X00 总体简介 云优CMS于2017年9月上线全新版本,二级域名分站,内容分站独立,七牛云存储,自定义字段,自定义表单,自定义栏目权限,自定义管理权限等众多功能深受用户青睐,上线短短3个月,下载次 ...

  9. 快速排序的C++实现

    版权声明:本文为博主原创文章,未经博主允许不得转载. 快速排序的C++实现 int Partition(int a[], int low, int high) { int x = a[high];// ...

  10. Xtreme9.0 - Car Spark 动态规划

    Car Spark 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...