• 图论例题2——寻找道路


    题目描述

    在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:

    1 .路径上的所有点的出边所指向的点都直接或间接与终点连通。

    2 .在满足条件1 的情况下使路径最短。

    注意:图G 中可能存在重边和自环,题目保证终点没有出边。

    请你输出符合条件的路径的长度。

    输入输出格式

    输入格式:

    输入文件名为road .in。

    第一行有两个用一个空格隔开的整数n 和m ,表示图有n 个点和m 条边。

    接下来的m 行每行2 个整数x 、y ,之间用一个空格隔开,表示有一条边从点x 指向点y 。

    最后一行有两个用一个空格隔开的整数s 、t ,表示起点为s ,终点为t 。

    输出格式:

    输出文件名为road .out 。

    输出只有一行,包含一个整数,表示满足题目᧿述的最短路径的长度。如果这样的路径不存在,输出- 1 。

    输入输出样例

    输入样例#1:

    3 2 

    1 2 

    2 1 

    1 3 

    输出样例#1:

    -1

    输入样例#2:

    6 6 

    1 2 

    1 3 

    2 6 

    2 5 

    4 5 

    3 4 

    1 5 

    输出样例#2:

    3

    说明

    解释1:

     

    如上图所示,箭头表示有向道路,圆点表示城市。起点1 与终点3 不连通,所以满足题目描述的路径不存在,故输出- 1 。

    解释2:

     

    如上图所示,满足条件的路径为1 - >3- >4- >5。注意点2 不能在答案路径中,因为点2连了一条边到点6 ,而点6 不与终点5 连通。

    对于30%的数据,0<n≤10,0<m≤20;

    对于60%的数据,0<n≤100,0<m≤2000;

    对于100%的数据,0<n≤10,000,0<m≤200,000,0<x,y,s,t≤n,x≠t。

    Answer:

     1 #include <iostream>
     2 #include <fstream>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdio>
     6 #include <cstdlib>
     7 #include <algorithm>
     8 #include <cmath>
     9 #include <bitset>
    10 #include <ctime>
    11 #include <map>
    12 #include <queue>
    13 #include <set>
    14 using namespace std;
    15  
    16 const int maxn=10000+15;
    17 const int maxm=200000+15;
    18 int n,m,x,y,S,T;
    19 int fr1,nod1[maxm],nex1[maxm],hea1[maxn];
    20 int fr2,nod2[maxm],nex2[maxm],hea2[maxn];
    21 bool boo[maxn],bog[maxn];
    22 int h,t,line[maxn];
    23 int f[maxn];
    24 int ins1(int x,int y)
    25 {
    26     nod1[++fr1]=y;
    27     nex1[fr1]=hea1[x];
    28     hea1[x]=fr1;
    29     return 0;
    30 }
    31 int ins2(int x,int y)
    32 {
    33     nod2[++fr2]=y;
    34     nex2[fr2]=hea2[x];
    35     hea2[x]=fr2;
    36     return 0;
    37 }
    38 int bfs2(int now)
    39 {
    40     line[h=t=1]=now;
    41     bog[now]=true;
    42     for (;h<=t;h++)
    43      for (int u=hea2[line[h]];u;u=nex2[u])
    44       if (!bog[nod2[u]])
    45       {
    46         bog[nod2[u]]=true;
    47         line[++t]=nod2[u];
    48       }
    49     for (int i=1;i<=n;i++)
    50     {
    51         bool bo=true;
    52         for (int u=hea1[i];u;u=nex1[u])
    53          if (!bog[nod1[u]])
    54          {
    55             bo=false;
    56             break;
    57          }
    58         boo[i]=bo;
    59     }
    60     return 0;
    61 }
    62 int bfs(int now)
    63 {
    64     memset(f,-1,sizeof(f));
    65     if (!boo[now])
    66     {
    67         printf("-1
    ");
    68         return 0;
    69     }
    70     line[h=t=1]=now;
    71     f[now]=0;
    72     for (;h<=t;h++)
    73      for (int u=hea1[line[h]];u;u=nex1[u])
    74       if (f[nod1[u]]==-1 && boo[nod1[u]])
    75       {
    76         f[nod1[u]]=f[line[h]]+1;
    77         line[++t]=nod1[u];
    78       }
    79     printf("%d
    ",f[T]);
    80     return 0;
    81 }
    82 int main()
    83 {
    84     scanf("%d%d",&n,&m);
    85     for (int i=1;i<=m;i++)
    86     {
    87         scanf("%d%d",&x,&y);
    88         ins1(x,y);
    89         ins2(y,x);
    90     }
    91     scanf("%d%d",&S,&T);
    92     bfs2(T);
    93     bfs(S);
    94     return 0;
    95 }
  • 相关阅读:
    使用 RAISERROR
    简单计算 2个日期 相差的 天数 和 小时数
    一个 SQL 分段排序的问题(也可以理解为 段内汇总排序问题)
    清除数据库里面所有用户表的数据
    SQL for xml path 应用 很详细
    mybatis读取配置文件报错:Could not find resource configuration.xml
    Attribute "resource" must be declared for element type "mapper".
    Mac OS X Common Lisp环境配置
    【自己习惯太差】最近看书的总结(1),慢慢改正自己的习惯
    LMS权值更新法则
  • 原文地址:https://www.cnblogs.com/9pounds15pence/p/6349660.html
Copyright © 2020-2023  润新知