URAL 1837. Isenbaev's Number (map + Dijkstra || BFS)
1837. Isenbaev's Number
Memory limit: 64 MB
have solved a problem. Maybe, even two…
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…
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
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
Sample
input | output |
---|---|
7 |
Ayzenshteyn 2 |
题意:给出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's Number (map + Dijkstra || BFS)的更多相关文章
- 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...
- Find the duplicate Number (鸽巢原理) leetcode java
问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- c++的关联容器入门(map and set)
目录 std::map std::set C++的关联容器主要是两大类map和set 我们知道谈到C++容器时,我们会说到 顺序容器(Sequence containers),关联容器(Associa ...
- number(4,2)
number(4,2) ##.## 例如:45.23 number(6,2)就是####.## 例如:9994.11 4代表总共有效位数为4位2代表小数位为2位
- <LeetCode OJ> 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 ...
- HDU2665Kth number (主席树+离散)
Give you a sequence and ask you the kth big number of a inteval. InputThe first line is the number o ...
- 2019浙大校赛--J--Extended Twin Composite Number(毒瘤水题)
毒瘤出题人,坑了我们好久,从基本的素数筛选,到埃氏筛法,到随机数快速素数判定,到费马小定理,好好的水题做成了数论题. 结果答案是 2*n=n+3*n,特判1,2. 以下为毒瘤题目: 题目大意: 输入一 ...
- 白话空间统计之:Moran's I(莫兰指数)
前两天聊了空间统计学里面的两个经典概念,今天来说说第一篇文章留下的大坑:Moran's I. 首先,Moran's I这个东西.官方叫做:莫兰指数,是澳大利亚统计学家帕特里克·阿尔弗雷德·皮尔斯·莫兰 ...
- 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 ...
随机推荐
- HTTPS-加密SSL证书
从第一部分HTTP工作原理中,我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密码.在握手过程中,网站会向浏览器发送SSL证书,SSL证书和我们日常用的身份证类似 ...
- 局域网搭建https局域网
局域网搭建https局域网 1.使用tomcat作为服务器搭建局域网访问https 需要使用java jdk\bin下的keytool.exe来创建证书 使用命令:keytool -genkenpai ...
- java 内部类和向上转型
当将内部类向上转型为其基类时,尤其是转型为一个接口的时候,内部类就有了用武之地,(从实现某个接口的对象,得到对接口的引用,与向上转型为这个对象的基类,实际上是一样的效果,),这是因为此内部类---某个 ...
- Linux学习笔记:常用命令grep、iconv、cp、mv、rm
本篇记录一些近期常用的命令. 一.grep过滤 grep过滤 不包含某些字符串 cat test.txt | grep -v '.jpg' 过滤jpg结尾的图片 cat test.txt | grep ...
- DateTime格式字符串HH与hh
早上写了一个关于接口的示例代码,结果发现了一个bug.接口中我内部将DateTime转化为12小时进制的字符串,这样就导致在用户没有指定小时时,不会默认写入00,而是12. 例如: DateTime ...
- LOJ 10160 - 「一本通 5.2 练习 3」周年纪念晚会 / 没有上司的晚会
题面 传送门 Ural 州立大学的校长正在筹备学校的 8080 周年纪念聚会.由于学校的职员有不同的职务级别,可以构成一棵以校长为根的人事关系树.每个资源都有一个唯一的整数编号,从 $1$ 到 $N$ ...
- fiddler抓https 关于证书一项
由于我之前电脑装过fiddler然后这次弄证书也没有理解明白,导致失败了.然后就百度到了下面的教程. https://www.cnblogs.com/joshua317/p/8670923.html ...
- 【LOJ】#2527. 「HAOI2018」染色
题解 简单容斥题 至少选了\(k\)个颜色恰好出现\(S\)次方案数是 \(F[k] = \binom{M}{k} \frac{N!}{(S!)^{k}(N - i * S)!}(M - k)^{N ...
- P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...
- 【Ray Tracing The Next Week 超详解】 光线追踪2-3
Preface 终于到了激动人心的纹理章节了 然鹅,看了下,并不激动 因为我们之前就接触过 当初有一个 attenuation 吗? 对了,这就是我们的rgb分量过滤器,我们画出的红色.蓝色.绿色等 ...