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 }
  • 相关阅读:
    ubuntu18.04英文环境解决各种软件中文乱码问题
    Centos6两个镜像文件的合并方法
    将centos的yum源修改为阿里云的yum源
    Linux TOP 命令总结
    Nginx add SSL 证书 基础配置
    Nginx Http 核心模块中Server Location 配置
    df -h执行卡住不动问题解决
    Jetty Session Persistence By Redis
    Spring @Transactional配置知识梳理
    通用FTP Client模块设计与实现
  • 原文地址:https://www.cnblogs.com/quintessence/p/6676147.html
Copyright © 2011-2022 走看看