URAL 1837. Isenbaev's Number (map + Dijkstra || BFS)

来源:互联网 发布:王者荣耀自慰软件 编辑:程序博客网 时间:2024/05/18 02:51

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

inputoutput
7Isenbaev Oparin ToropovAyzenshteyn Oparin SamsonovAyzenshteyn Chevdar SamsonovFominykh Isenbaev OparinDublennykh Fominykh IvankovBurmistrov Dublennykh KurpilyanskiyCormen Leiserson Rivest
Ayzenshteyn 2Burmistrov 3Chevdar 3Cormen undefinedDublennykh 2Fominykh 1Isenbaev 0Ivankov 2Kurpilyanskiy 3Leiserson undefinedOparin 1Rivest undefinedSamsonov 2Toropov 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 1e7const 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;}




0 0
原创粉丝点击