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

Source: Internet
Author: User

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, and absolute champion of ACM ICPC The time you'll spend reading this problem statement Vladislav would has solved a problem. Maybe, even ... Since Vladislav Isenbaev graduated from the Specialized educational and scientific Center at Ural State University, many O f The former and present contestants at USU has known him for quite a few years. Some of them is proud to say, 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 numberas 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 he teammates but played in the same team with one or more of the He teammates, the number is 2, and S O on. Your task is to automate the process of calculating Isenbaev ' s numbers so, each contestant at USU would know pro Ximity to the ACM ICPC champion. Inputthe First line contains the number of teams N(1≤ N≤100). In the following NLines you is given the names of the three members of the corresponding team. The names is separated with a space. Each of the name is a nonempty line consisting of 中文版 letters, and its length are at the most symbols. The first letter of a name was capital and the other letters was lowercase. Outputfor 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
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






Test instructions: Given n 3-person group, Isenbaev is numbered 0, his teammate number is 1, his teammates ' teammates are numbered 2 ... And so on, if there is no way to contact Isenbaev through a relationship, the output "undefined", the other output number.

Parsing: First map out all the people with a map number, here to use the map can be automatically sorted by the dictionary order characteristics, all occurrences of the string directly into the map, traversing the time is a dictionary order. Then is the number of vertices to build a non-map, the distance between the members of 1, so that the Dijkstra can be solved by the shortest distance. Of course, BFS can also search the shortest path length.



AC Code:

#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 algorithm 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;          Put the string in map} int num = 0;        Map<string, int>::iterator it;                             for (it = M.begin (); It!=m.end (); it++) {it->second = ++num;        Give a string number} 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++) {//Initialize the members of the group between the distance 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" &            lt;<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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.