ural 1837 Isenbaev's Number

http://acm.timus.ru/problem.aspx?space=1&num=1837

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <vector>
  4 #include <string>
  5 #include <iostream>
  6 #include <queue>
  7 #include <algorithm>
  8 using namespace std;
  9 vector<int>q[500];
 10 
 11 struct node
 12 {
 13     string s;
 14     int num;
 15     bool operator <(const node &a)const
 16     {
 17         return s<a.s;
 18     }
 19 } p1[500*3];
 20 int t1;
 21 
 22 int getnum(string c)
 23 {
 24     for(int i=0; i<t1; i++)
 25     {
 26         if(c==p1[i].s) return i;
 27     }
 28     p1[t1++].s=c;
 29     return t1-1;
 30 }
 31 
 32 void bfs(int str)
 33 {
 34     queue<int>qq;
 35     bool vis[50000];
 36     memset(vis,false,sizeof(vis));
 37     qq.push(str);
 38     p1[str].num=0;
 39     vis[str]=true;
 40     while(!qq.empty())
 41     {
 42         int m=qq.front();
 43         qq.pop();
 44         for(int j=0; j<q[m].size(); j++)
 45         {
 46             int x=q[m][j];
 47             vis[x]=true;
 48             if(p1[x].num==-1111){
 49             p1[x].num=p1[m].num+1;
 50             qq.push(x);
 51             }
 52         }
 53     }
 54 }
 55 
 56 int main()
 57 {
 58     int t;
 59     scanf("%d",&t);
 60     string s1,s2,s3;
 61     t1=0;
 62     int k1,k2,k3;
 63     for(int i=1; i<=t; i++)
 64     {
 65         cin>>s1>>s2>>s3;
 66         k1=getnum(s1);
 67         k2=getnum(s2);
 68         k3=getnum(s3);
 69         q[k1].push_back(k2);
 70         q[k1].push_back(k3);
 71         q[k2].push_back(k1);
 72         q[k2].push_back(k3);
 73         q[k3].push_back(k1);
 74         q[k3].push_back(k2);
 75     }
 76     for(int j=0; j<t1; j++)
 77     {
 78         p1[j].num=-1111;
 79     }
 80     string s4="Isenbaev";
 81     int num1;
 82     bool flag=false;
 83     for(int j=0; j<t1; j++)
 84     {
 85         if(p1[j].s==s4)
 86         {
 87             flag=true;
 88             num1=j;
 89             break;
 90         }
 91     }
 92     if(flag)
 93     bfs(num1);
 94     sort(p1,p1+t1);
 95     for(int i=0; i<t1; i++)
 96     {
 97         if(p1[i].num==-1111)
 98         {
 99             cout<<p1[i].s<<" "<<"undefined"<<endl;
100         }
101         else
102              cout<<p1[i].s<<" "<<p1[i].num<<endl;
103     }
104     return 0;
105 }
View Code

 

posted @ 2014-03-18 21:09  null1019  阅读(165)  评论(0编辑  收藏  举报