POJ 1986Distance Queries tarjan求LCA

来源:互联网 发布:linux 解压 zip 装什么 编辑:程序博客网 时间:2024/05/16 11:02

题目:http://poj.org/problem?id=1986


题意:给定一个树,求树上两点间的最短距离


思路;tarjan求LCA,然后dist[v,u] = dist[v] + dist[u] - 2 * dist[LCA(v,u)]


总结:不知输入数据里的字母是什么鬼。。。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 40010;typedef long long ll;struct edge{    int to, cost, next;}G1[N*2];struct node{    int to, ind, next;}G2[N];int head1[N], head2[N], par[N];int cnt1, cnt2;ll dist[N], res[N];bool vis[N];int n, m;void init(){    for(int i = 1; i <= n; i++)        par[i] = i;    memset(head1, -1, sizeof head1);    memset(head2, -1, sizeof head2);    memset(vis, 0, sizeof vis);    cnt1 = cnt2 = 0;}void add_edge1(int v, int u, int c){    G1[cnt1].to = u;    G1[cnt1].cost = c;    G1[cnt1].next = head1[v];    head1[v] = cnt1++;}void add_edge2(int v, int u, int ind){    G2[cnt2].to = u;    G2[cnt2].ind = ind;    G2[cnt2].next = head2[v];    head2[v] = cnt2++;}int ser(int x){    int r = x, i = x, j;    while(r != par[r]) r = par[r];    while(i != r) j = par[i], par[i] = r, i = j;    return r;}void tarjan_lca(int v){    vis[v] = true;    int u;    for(int i = head1[v]; i != -1; i = G1[i].next)        if(!vis[u = G1[i].to])        {            dist[u] = dist[v] + G1[i].cost;            tarjan_lca(u);            par[u] = v;        }    for(int i = head2[v]; i != -1; i = G2[i].next)        if(vis[u = G2[i].to])            res[G2[i].ind] = dist[v] + dist[u] - 2 * dist[ser(u)];}int main(){    int k, a, b, c;    char ch;    while(~scanf("%d%d", &n, &m))    {        init();        for(int i = 0; i < m; i++)        {            scanf("%d%d%d %c", &a, &b, &c, &ch);            add_edge1(a, b, c);            add_edge1(b, a, c);        }        scanf("%d", &k);        for(int i = 0; i < k; i++)        {            scanf("%d%d", &a, &b);            add_edge2(a, b, i);            add_edge2(b, a, i);        }        dist[1] = 0;        tarjan_lca(1);        for(int i = 0; i < k; i++)            printf("%lld\n", res[i]);    }    return 0;}


0 0
原创粉丝点击