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\n", Res[i]);
        }
    }

    return 0;
}

  

 

转载于:https://www.cnblogs.com/YDDDD/p/10837711.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值