• 最少转机


    Description

    小哼和小哈一同坐飞机去旅游,他们现在位于1号城市,目标是5号城市,可是1号城市并没有到5号城市的直航。不过小哼已经收集了很多航班的信息,现在小哼希望找到一种乘坐方式,使得转机的次数最少,如何解决呢?

    Input

    第一行的有两个整数n m s e,n表示有n个城市(城市编号为1~n),m表示有m条航线,s表示起点城市,e表示目标城市。
    接下来m行每行是一条类似“a b”这样的数据表示城市a和城市b之间有航线,也就是说城市a和城市b之间可以相互到达

    Output

    s号城市到e号目标城市,需要飞行几次?

    1<=n<=1000, 1<=m<=300000

    Sample Input

    5 7 1 5
    1 2
    1 3
    2 3
    2 4
    3 4
    3 5
    4 5
    

    Sample Output

    2
    

     1 #include<iostream>
     2 #include<fstream>
     3 #define inf 0x3f3f3f3f
     4 using namespace std;
     5 int n,st,en;
     6 long long m;
     7 int mina=inf;
     8 int book[1020],e[1030][1030];
     9 void dfs(int cur,int num){
    10     if(cur==en)//如果到了指定城市 
    11         mina=min(mina,num);//选取最小的转机次数 
    12     for(int i=1;i<=n;i++){//cur城市与各个城市相比 
    13         if(e[cur][i]==1&&book[i]==0){//如果有路并且没有标记过 
    14             book[i]=1;//标记 
    15             dfs(i,num+1);//接着搜索 
    16             book[i]=0;//解除标记 
    17         } 
    18     }    
    19 }
    20 int main(){
    21     fstream file("haha.txt");
    22     file>>n>>m>>st>>en;
    23     for(int i=1;i<=n;i++){//初始化 
    24         for(int j=1;j<=n;j++){
    25             if(i==j)
    26                 e[i][j]=0;
    27             else
    28                 e[i][j]=inf;    
    29         }
    30     }
    31     for(int i=1;i<=m;i++){//录入信息 
    32         int a,b;
    33         file>>a>>b;
    34         e[a][b]=1;
    35     }
    36     book[st]=1;
    37     dfs(st,0);//开始搜索 
    38     cout<<mina;
    39     return 0;
    40 }
    注意一下,尽量不要把变量名设置成end,在oj上会报错。
    Main.cc: In function void dfs(int, int)’:
    Main.cc:11:10: error: reference to end is ambiguous
      if(cur==end)
              ^
    Main.cc:7:11: note: candidates are: int end
     int start,end=0;
               ^




  • 相关阅读:
    在Eclipse/STS里添加代码反编译器(.class)步骤
    关于页面添加字段
    关于jeesite Date和时间戳问题
    关于前端ajax通过实体类向后端传参报不存在问题
    引入字典
    关于添加主键
    简单的前后端分离项目 部署到 centos7
    Cenost7 Mysql5.7 安装 并打开远程访问
    npm install vue-cli -g 报错
    docker 笔记 docker 基础 docker 常用命令
  • 原文地址:https://www.cnblogs.com/fate-/p/12242328.html
Copyright © 2020-2023  润新知