zoukankan      html  css  js  c++  java
  • [SOJ] can I post the letter?

    1155. Can I Post the letter

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    I am a traveler. I want to post a letter to Merlin. But because there are so many roads I can walk through, and maybe I can’t go to Merlin’s house following these roads, I must judge whether I can post the letter to Merlin before starting my travel. 

    Suppose the cities are numbered from 0 to N-1, I am at city 0, and Merlin is at city N-1. And there are M roads I can walk through, each of which connects two cities. Please note that each road is direct, i.e. a road from A to B does not indicate a road from B to A.

    Please help me to find out whether I could go to Merlin’s house or not.

    Input

    There are multiple input cases. For one case, first are two lines of two integers N and M, (N<=200, M<=N*N/2), that means the number of citys and the number of roads. And Merlin stands at city N-1. After that, there are M lines. Each line contains two integers i and j, what means that there is a road from city i to city j.

    The input is terminated by N=0.

    Output

    For each test case, if I can post the letter print “I can post the letter” in one line, otherwise print “I can't post the letter”.

    Sample Input

    3
    2
    0 1
    1 2
    3
    1
    0 1
    0
    

    Sample Output

    I can post the letter
    I can't post the letter


    利用Dijiskra算法解决问题

    #include<iostream>
    #include<memory>
    using namespace std;
    
    const int MAX = 205;
    int edge[MAX][MAX];
    bool visited[MAX];
    int n, m;
    
    void DFS(int current)
    {
       for(int i=0;i<n;i++)
       {
        if(!visited[i]&&edge[current][i])
        {
          visited[i]=true;
          DFS(i);
        }
       }
    }
    
    int main()
    {
      while(cin>>n&&n!=0&&cin>>m)
      {
        memset(edge, 0, sizeof(edge));
        memset(visited, false, sizeof(visited));
        int a, b;
    
        for(int i=0;i<m;i++)
        {
          cin>>a>>b;
          edge[a][b]=1;
          edge[b][a]=1;
        }
        visited[0]=true;
    
        DFS(0);
    
        if(visited[n-1])cout<<"I can post the letter
    ";
        else cout<<"I can't post the letter
    ";
      }
    
      return 0;
    }
    
  • 相关阅读:
    mongodb06---索引
    mongodb05---游标
    mongo04---基本查询
    mysql06---权限控制
    mysql05---游标
    使用 inotifywait的方式监控文件夹发生变化后自动执行脚本的方法
    ubuntu18.04 安装wine以及添加mono和gecko打开简单.net应用的方法
    Android之Socket群组聊天
    史上最完整的Android开发工具集合
    Android SurfaceView使用详解
  • 原文地址:https://www.cnblogs.com/KennyRom/p/6245769.html
Copyright © 2011-2022 走看看