简体   繁体   English

C ++:BFS计算树中每对节点之间的距离

[英]C++ : BFS to calculate distance between every pair of nodes in a tree

I have a weighted tree with N vertices stored in form of adjacency list. 我有一棵以邻接表形式存储的N个顶点的加权树。 I have a list of M nodes. 我有M个节点的列表。

Now to calculate distance between every pair of nodes from the list of M nodes in this tree I wrote this : 现在要从该树中的M个节点列表中计算出每对节点之间的距离,我这样写:

using namespace std;
#define MAX_N (1<<17)
#define MAX_V (1<<17)

typedef pair<int,int> pii;    
vector<pii> adj[MAX_V];

bool vis[MAX_N]; //mark the node if visited 
ll bfs(pii s,pii d)
{
    queue <pii> q;
    q.push(s);
    ll dist=0;

    vis[ s.first ] = true;
    while(!q.empty())
    {
        pii p = q.front();
        q.pop();
        for(auto i = 0 ; i < adj[ p ].size() ; i++)
        {
            if(vis[ adj[ p ][ i ].first ] == false)
            {
                 q.push(adj[ p ][ i ].first);
                 vis[ adj[ p ][ i ] ] = true;
                 dist += adj[p][i].second;
            }


        }
    }

    return dist;
}

 int main()
 {

        for(int i=0;i<N;i++)
        {
            int v1,v2,l;
            cin>>v1>>v2>>l;
            adj[v1].push_back(make_pair(v2,l));
           //  adj[v2].push_back(make_pair(v1,l));
        }
        int a[M];
        for(int i=0;i<M;i++)
        cin >> a[i];

        int ans=0;


      for(int i=0;i<M-1;i++)
        {
            for(int j=i+1;j<M;j++)
            {
                num += bfs(adj[a[i]],adj[a[j]]);
            }
        }

  }

The program doesn't compile and the error is as follows : 该程序无法编译,错误如下:

could not convert 'adj[a[i]]' from 'std::vector<std::pair<long long int, long long int> >' to 'pii {aka std::pair<long long int, long long int>}'
              num += bfs(adj[a[i]],adj[a[j]]);

Also I know this program is wrong while calculating BFS because it isn't stopping when it reaches the destination vertex. 我也知道该程序在计算BFS时出错,因为它到达目标顶点时不会停止。

Can someone help me out in correcting these errors ? 有人可以帮我纠正这些错误吗?

I think there are several issues: 我认为有几个问题:

  • for(auto i = 0 ; i < adj[ p ].size() ; i++) you are using p as index for adj, but p is of type pii . for(auto i = 0 ; i < adj[ p ].size() ; i++)您使用p作为adj的索引,但是p是pii类型。 You probably want p.first, assuming that the pairs have meaning (from, to) 您可能需要p.first,假设这两个对具有(from,to)的含义
  • if(vis[ adj[ p ][ i ].first ] == false) : I assume you want to do check if the neighbours are not visited yet. if(vis[ adj[ p ][ i ].first ] == false) :我假设您想检查邻居是否尚未访问。 So it should more something like this: vis[adj[p.first][i].second] == false . 因此,它应该像这样: vis[adj[p.first][i].second] == false The index for visited is adj[p.first][i].second . 访问的索引是adj[p.first][i].second I am checking second because I assume the semantic for (from, to) for the pair 我正在检查第二个原因,因为我假设该对的语义(从,到)
  • q.push(adj[ p ][ i ].first); : you are pushing an integer, but the queue holds type pii . :您正在推送一个整数,但是队列持有类型pii Up to you how you want to change it. 由您决定如何更改它。
  • dist += adj[p][i].second; : you are indexing the array using the pair. :您正在使用一对索引数组。 You should use an index 您应该使用索引
  • Finally num += bfs(adj[a[i]],adj[a[j]]); 最后num += bfs(adj[a[i]],adj[a[j]]); as Buckster already explained in the comment you are passing vector<pii> instead of pii to the the bfs function 如Buckster在注释已经说明的要传递vector<pii>代替pii到的bfs功能

These are only compile issues though. 这些只是编译问题。 I am not sure your algorithms actually does what you expect. 我不确定您的算法是否确实能达到您的期望。 You can use bfs to compute the distance between any couple of node, but if it is weighted, bfs per se does not give you the minimum path. 您可以使用bfs计算任意两个节点之间的距离,但是如果对其加权,bfs本身不会给您最小的路径。 If you are interested in the minimum path, you can use Dijkstra, provided that weights are positive. 如果您对最小路径感兴趣,可以使用Dijkstra,只要权重为正。 Here I have a bfs implementation that you can check, if you want, but it slightly more complex of what you are trying to do here. 在这里,我有一个bfs实现,您可以根据需要进行检查,但是它要在这里尝试执行的操作稍微复杂一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM