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 }
  • 相关阅读:
    windows phone 8 更换锁屏界面图片
    Kinect for Windows 入门与开发
    Windows 8 学习
    windows phone 8 锁屏界面 显示应用程序的消息提醒
    Windows 8 如何在后台播放音频
    C# 4.0 的 Visual Studio 2010 示例
    windows phone 页面切换 动画
    Windows Phone 官方示例学习:Short message dictation and web search grammars sample(语音识别,文字语音)
    Windows Phone 官方示例学习:Background Transfer Service Sample(后台传输)
    打日志流程
  • 原文地址:https://www.cnblogs.com/quintessence/p/6676147.html
Copyright © 2011-2022 走看看