POJ-1986-Distance Queries

链接:https://vjudge.net/problem/POJ-1986#author=0

题意:

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

一个无向图求两个点的最短路。

思路:

Targjan算法,同时在记录查询的时候需要用链式结构,如果每次dfs都k次查找会T。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std;

typedef long long LL;
const int MAXN = 4e4+10;
struct Node
{
    int from_, to_, dist_;
    Node(int from, int to, int dist):from_(from), to_(to), dist_(dist){}
};
vector<Node> G[MAXN];
vector<Node> Q[MAXN];
int fa[MAXN], dis[MAXN];
int vis[MAXN], fas[MAXN];
int Res[MAXN];
int n, m, l, r, v, k;
int root, res;

int Get_F(int x)
{
    if (fa[x] == x)
        return x;
    fa[x] = Get_F(fa[x]);
    return fa[x];
}

void Merge(int u, int v)
{
    int tv = Get_F(v);
    int tu = Get_F(u);
    if (tv != tu)
        fa[v] = u;
}

void Tarjan(int u)
{
    vis[u] = 1;
    for (int i = 0;i < Q[u].size();i++)
    {
        Node node = Q[u][i];
        if (vis[node.to_] == 1)
        {
            int lenth = dis[node.from_]+dis[node.to_]-2*dis[Get_F(node.to_)];
            Res[node.dist_] = lenth;
        }
    }
    for (int i = 0;i < G[u].size();i++)
    {
        Node node = G[u][i];
        if (!vis[node.to_])
        {
            dis[node.to_] = dis[node.from_] + node.dist_;
            Tarjan(node.to_);
            Merge(node.from_, node.to_);
        }
    }
}

void init()
{
    for (int i = 1;i <= n;i++)
    {
        G[i].clear();
        fa[i] = i;
    }
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(vis));
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    //cin >> t;
    //while (t--)
    while(~scanf("%d%d", &n, &m))
    {
//        cin >> n >> m;
//        scanf("%d%d", &n, &m);
        char op;
        init();
        for (int i = 1;i <= m;i++)
        {
//            cin >> l >> r >> v >> op;
            scanf("%d%d%d %c", &l, &r, &v, &op);
            G[l].push_back(Node(l, r, v));
            G[r].push_back(Node(r, l, v));
        }
        scanf("%d", &k);
        for (int i = 1;i <= k;i++)
        {
//            cin >> l >> r;
            scanf("%d%d", &l, &r);
            Q[l].push_back(Node(l, r, i));
            Q[r].push_back(Node(r, l, i));
        }
        dis[1] = 0;
        Tarjan(1);
        for (int i = 1;i <= k;i++)
        {
            printf("%d
", Res[i]);
        }
    }

    return 0;
}

  

 

原文地址:https://www.cnblogs.com/YDDDD/p/10837711.html