最短路径:Shortest Reach

        Given an undirected graph consisting of  nodes (labelled 1 to N) where a specific given node  represents the start position and an edge between any two nodes is of length  units in the graph. It is required to calculate the shortest distance from start position (Node S) to all of the other nodes in the graph.

     Note 1: If a node is unreachable , the distance is assumed as 
     Note 2: The length of each edge in the graph is  units.

    Input Format

   The first line contains , denoting the number of test cases. 
   First line of each test case has two integers , denoting the number of nodes in the graph and , denoting the number of edges in the graph. 
The next  lines each consist of two space separated integers , where  and  denote the two nodes between which the edge exists. 
The last line of a testcase has an integer , denoting the starting position.

Constraints 
 Output Format

For each of  test cases, print a single line consisting of  space-separated integers, denoting the shortest distances of the N-1 nodes from starting position . This will be done for all nodes same as in the order of input 1 to N.

For unreachable nodes, print .

Sample Input

2
4 2
1 2
1 3
1
3 1
2 3
2

Sample Output

6 6 -1
-1 6

Explanation

For test cases 1:

The graph given in the test case is shown as :

Graph

S denotes the node 1 in the test case and B,C and D denote 2,3 and 4. Since S is the starting node and the shortest distances from it are (1 edge, 1 edge, Infinity) to the nodes B,C and D (2,3 and 4) respectively.

Node D is unreachable, hence -1 is printed (not Infinity).

For test cases 2: There are only one edge (2, 3) in a graph with 3 nodes, so node 1 is unreachable from node 2, and node 3 has one edge from node 2, each edge has the length of 6 units. So we output -1 6.

解题方法一:弗洛伊德算法,结果超时:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
unsigned int MAXVALUE = 5000001;
#define MAX 1001
unsigned int map[MAX][MAX];
unsigned int N,M;
unsigned int dist[MAX];
unsigned int T;
unsigned int visited[MAX];
int queue[MAX*MAX];
int main() 
{
    cin >> T;
    for(int i = 1;i<=T;i++)
    {
       cin>>N>>M;
        for(int index =1; index <=N;index++)
        {
            for(int x = 1;x<=N;x++)
            {
              map[index][x] = MAXVALUE;    
            }
            visited[index] = 0;
            dist[index] = 0;
        }
       for(int j =1; j<= M;j++)
       {
           int x,y;
           cin>>x>>y;
           map[x][y] = 1;
           map[y][x] = 1;   
       }
       int S;
       cin>>S;
       int head ,tear = 0;
       head = 0;
       visited[S] = 1;
       queue[tear++] = S;
       dist[S] = 0;
       while(head != tear)
       {
          int cur = queue[head];
          for(int i = 1 ; i<=N;i++)
          {
              if(map[cur][i] == 1 && visited[i] == 0)
              {
                   dist[i] = dist[cur] + 6;
                   visited[i] = 1;
                   queue[tear++] = i;
              }      
          }
          head++;
        }
       for(int index0 = 1; index0 <=N;index0++)
       {
          if( index0 !=S)
          {
               if(dist[index0] != 0)
              {
                 cout<<dist[index0]<<" ";
              }
              else
              {
              cout<<"-1 ";
              }
          }
         
       }
       cout<<endl;
    }/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

迪杰斯特拉:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
unsigned int MAXVALUE = 5000001;
#define MAX 1001
unsigned int map[MAX][MAX];
unsigned int N,M;
unsigned int dist[MAX];
unsigned int T;
unsigned int visited[MAX];
int queue[MAX*MAX];
int main() 
{
    cin >> T;
    for(int i = 1;i<=T;i++)
    {
       cin>>N>>M;
        for(int index =1; index <=N;index++)
        {
            for(int x = 1;x<=N;x++)
            {
              map[index][x] = MAXVALUE;    
            }
            visited[index] = 0;
            dist[index] = 0;
        }
       for(int j =1; j<= M;j++)
       {
           int x,y;
           cin>>x>>y;
           map[x][y] = 1;
           map[y][x] = 1;   
       }
       int S;
       cin>>S;
       int head ,tear = 0;
       head = 0;
       visited[S] = 1;
       queue[tear++] = S;
       dist[S] = 0;
       while(head != tear)
       {
          int cur = queue[head];
          for(int i = 1 ; i<=N;i++)
          {
              if(map[cur][i] == 1 && visited[i] == 0)
              {
                   dist[i] = dist[cur] + 6;
                   visited[i] = 1;
                   queue[tear++] = i;
              }      
          }
          head++;
        }
       for(int index0 = 1; index0 <=N;index0++)
       {
          if( index0 !=S)
          {
               if(dist[index0] != 0)
              {
                 cout<<dist[index0]<<" ";
              }
              else
              {
              cout<<"-1 ";
              }
          }
         
       }
       cout<<endl;
    }/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

BFS:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
unsigned int MAXVALUE = 5000001;
#define MAX 1001
unsigned int map[MAX][MAX];
unsigned int N,M;
unsigned int dist[MAX];
unsigned int T;
unsigned int visited[MAX];
int queue[MAX*MAX];
int main() 
{
    cin >> T;
    for(int i = 1;i<=T;i++)
    {
       cin>>N>>M;
        for(int index =1; index <=N;index++)
        {
            for(int x = 1;x<=N;x++)
            {
              map[index][x] = MAXVALUE;    
            }
            visited[index] = 0;
            dist[index] = 0;
        }
       for(int j =1; j<= M;j++)
       {
           int x,y;
           cin>>x>>y;
           map[x][y] = 1;
           map[y][x] = 1;   
       }
       int S;
       cin>>S;
       int head ,tear = 0;
       head = 0;
       visited[S] = 1;
       queue[tear++] = S;
       dist[S] = 0;
       while(head != tear)
       {
          int cur = queue[head];
          for(int i = 1 ; i<=N;i++)
          {
              if(map[cur][i] == 1 && visited[i] == 0)
              {
                   dist[i] = dist[cur] + 6;
                   visited[i] = 1;
                   queue[tear++] = i;
              }      
          }
          head++;
        }
       for(int index0 = 1; index0 <=N;index0++)
       {
          if( index0 !=S)
          {
               if(dist[index0] != 0)
              {
                 cout<<dist[index0]<<" ";
              }
              else
              {
              cout<<"-1 ";
              }
          }
         
       }
       cout<<endl;
    }/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值