Connections between cities HDU - 2874 (LCA)题解

4 篇文章 0 订阅

After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
Sample Input
5 3 2
1 3 2
2 4 3
5 2 3
1 4
4 5


题意很简单,就是找森林中两点的最短距离,要是在不同的树上,输出无连接。用的离线的tarjan的LCA解法,在dfs的过程中不进要记录离树根的距离,还要为这些结点标号,标明他们在哪棵树上。然而这道题目卡空间卡的厉害,因为询问的数量范围比较大,如何存图,存询问和存答案都必须注意的,改了我一个多小时,把邻接表存图和询问的方式改为前向星,然后单独开一个数组存答案,可以省去存lca和询问起点的空间。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
//const int mod = 1000000007;
const int maxm = 1000010;
const int maxn = 10002;
int n, m,c;
struct edge{
    int to, len, nex;
}e[20002];
int heade[maxn], headq[maxn];
int tot1, tot2;
struct que{
    int y,nex;
}q[2000002];
int vis[maxn];
int dis[maxn];
int ans[2000002];
int f[maxn];
void adde(int x, int y, int z){
    e[tot1].to = y;
    e[tot1].len = z;
    e[tot1].nex = heade[x];
    heade[x] = tot1++;
}
void addq(int x, int y){
    q[tot2].y = y;
    q[tot2].nex = headq[x];
    headq[x] = tot2++;
}

int F(int x){
    if (f[x] == x)return x;
    return f[x] = F(f[x]);
}

void dfs(int root,int u){
    vis[u] = root;
    for (int i = headq[u]; i!=-1; i=q[i].nex){
        int v = q[i].y;
        if (vis[v]){
            if (vis[v] != vis[u]){ ans[i/2] = -1; }
            else{ans[i/2] = dis[u]+dis[v]-2*dis[F(v)]; }
        }
    }
    for (int i = heade[u]; i!=-1; i=e[i].nex){
        int v = e[i].to;
        if (!vis[v]){
            dis[v] = dis[u] + e[i].len;
            dfs(root,v);
            f[v] = u;
        }
    }
}

int main() {
    int x, y,z;
    while (~scanf("%d%d%d", &n, &m, &c)){
        for (int i = 1; i <= n; i++){
            f[i] = i;
        }
        memset(heade, -1, sizeof(heade));
        memset(headq, -1, sizeof(headq));
        tot1 = tot2 = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < m; i++){
            scanf("%d%d%d", &x, &y, &z);
            adde(x, y, z);
            adde(y, x, z);
        }
        for (int i = 0; i < c; i++){
            scanf("%d%d", &x, &y);
            addq(x, y);
            addq(y, x);
        }
        int cnt = 1;
        for (int i = 1; i <= n; i++){
            if (!vis[i]){
                dis[i] = 0;
                dfs(cnt++, i);
            }
        }
        for (int i = 0; i < c; i++){
            if (ans[i] == -1){ printf("Not connected\n"); }
            else{
                printf("%d\n", ans[i]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值