zoukankan      html  css  js  c++  java
  • (最短路)SPOJ

    Ada the Ladybug is on a trip in Bugindia. There are many cities and some uni-directional roads connecting them. Ada is wondering about the shortest path, which begins in a city and ends in the same city. Since Ada likes short trips, she asked you to find the length of such path for each city in Bugindia.

    Input

    The first line will contain 0 < N ≤ 2000, the number of cities.

    Then N lines follow, each containing N integers 0 ≤ Hij ≤ 1. One means, that there is a road between and (zero means there isn't a road).

    Output

    Print N lines, the length of shortest path which begins in city i and ends in city i. If the path doesn't exist, print "NO WAY" instead.

    Example Input

    5
    0 1 1 1 1
    1 0 0 0 1
    0 0 1 1 0
    0 0 1 0 0
    0 0 0 1 0
    

    Example Output

    2
    2
    1
    2
    NO WAY
    

    Example Input

    5
    0 1 0 0 1
    0 0 1 0 0
    0 0 0 1 0
    0 0 0 0 1
    1 0 0 0 0
    

    Example Output

    2
    5
    5
    5
    2

    时间很宽松,因为每条边权值相同都为1,用最基础的bfs即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <stack>
    12 #define mp make_pair
    13 //#define INF 0x7fffffff
    14 typedef long long ll;
    15 typedef unsigned long long ull;
    16 const int MAX=2e3+5;
    17 const int INF=0x7fffffff;
    18 using namespace std;
    19 typedef pair<int,int> pii;
    20 int n;
    21 vector <int> g[MAX];
    22 queue <pii> que;
    23 bool vi[MAX];
    24 int tem,an;
    25 int bfs(int i)
    26 {
    27     while(!que.empty())
    28         que.pop();
    29     que.push(mp(0,i));
    30     while(!que.empty())
    31     {
    32         pii y=que.front();
    33         que.pop();
    34         int dis=y.first,id=y.second;
    35         for(int j=0;j<g[id].size();j++)
    36         {
    37             int to=g[id][j];
    38             if(!vi[to])
    39             {
    40                 vi[to]=true;
    41                 if(to==i)
    42                     return dis+1;
    43                 que.push(mp(dis+1,to));
    44             }
    45         }
    46     }
    47     return INF;
    48 }
    49 int main()
    50 {
    51     scanf("%d",&n);
    52     for(int i=1;i<=n;i++)
    53     {
    54         for(int j=1;j<=n;j++)
    55         {
    56             scanf("%d",&tem);
    57             if(tem)
    58                 g[i].push_back(j);
    59         }
    60     }
    61     for(int i=1;i<=n;i++)
    62     {
    63         an=INF;
    64         if(g[i].size())
    65         {
    66             memset(vi,false,n+1);
    67             an=bfs(i);
    68         }
    69         if(an==INF)
    70             printf("NO WAY
    ");
    71         else
    72             printf("%d
    ",an);
    73     }
    74 }
  • 相关阅读:
    nyoj-102-次方求模
    nyoj-420-p次方求和
    nyoj-93-汉诺塔(三)
    nyoj-684-Fox Ciel
    nyoj-148-fibonacci数列(二)
    测试面试话题3: 如何做好测试团队的管理者
    测试面试话题2: 给你一个测试团队,你会如何管理?
    测试面试话题1:敏捷开发与测试
    Github: 从github上拉取别人的源码,并推送到自己的github仓库
    Docker: Harbor一些小知识
  • 原文地址:https://www.cnblogs.com/quintessence/p/6676147.html
Copyright © 2011-2022 走看看