zoukankan      html  css  js  c++  java
  • TZOJ :2731: 存钱计划(二)

    描述

    在TZC,WY存了钱,现在他要去买东西了。店很多,标记为1,2,3,4,5,6....但有的店之间有大路相连,而有的没有路。现在要由一个店到另一个店买东西,中途最少要经过多少个其它的店铺呢?

    如图例,如果他从1开始到5,那么至少要经过1个店铺,从1到4至少要经过2个店铺。

    输入

    输入数据有多组,每组的第一行是两个正整数n, k(1<=n<=1000, 1<=k<=2000),接下来有k行,每行有两个正整数a, b(1 <= a, b <= n),表示店铺a和b之间有路相连,接下来一行为两个正整数p和q分别代表起始店铺。当n, k输入都为0时结束。

    输出

    输出从店铺p到店铺q之间最少要经过的其它的店铺的数目。如果从p无法到达q,则输出"No solution"。

    样例输入

    样例输出

    解题思路:BFS+记录步数

    菜鸡的成长史 ^-^

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N=1005;
     4 vector<int> G[N];
     5 int n,m,vis[N];
     6 struct Node
     7 {
     8     int B;
     9     int S;
    10 }p,q;
    11 void bfs(int start,int ending)
    12 {
    13     int flag=0;
    14     queue<Node> que;
    15     p.S=0,p.B=start;
    16     que.push(p);
    17     while(!que.empty())
    18     {
    19         q=que.front(),que.pop();
    20         int u=q.B,w=q.S;
    21         if(vis[u]) continue;
    22         vis[u]=1;
    23         for(auto X:G[u])
    24         {
    25             q.B=X,q.S=w+1;
    26             if(q.B==ending) {cout << q.S-1 << endl,flag=1;break;}
    27             que.push(q);
    28         }
    29         if(flag==1) break;
    30     }
    31     if(flag==0) cout << "No solution" << endl;
    32     while(!que.empty()) que.pop();
    33 }
    34 int main()
    35 {
    36     ios::sync_with_stdio(false);
    37     while(cin>>n>>m&&(n+m))
    38     {
    39         int d1,d2;
    40         while(m--){
    41             cin>>d1>>d2;
    42             G[d1].push_back(d2),G[d2].push_back(d1);
    43         }   //建图
    44         int start,ending;
    45         cin>>start>>ending;
    46         memset(vis,0,sizeof(vis));  //标记
    47         bfs(start,ending);
    48         for(int i=1;i<=n;i++) G[i].clear();  //注意清楚
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    人民币大小写转换
    window.parent与window.openner区别
    jquery.honver(),toggle()
    ADO.NET Entity Framework 之“无法加载指定的元数据资源。”
    window.external 的用法(收藏夹功能)
    空链接的技巧
    IIS自动开通组件编程
    在Asp.net中操作IIS的虚拟目录(C#)
    最佳 .NET 网站推荐
    边框 top、clientTop、scrollTop、offsetTop
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/10599426.html
Copyright © 2011-2022 走看看