当前位置: 代码迷 >> 综合 >> Connections between cities(HDU2874,LCA)
  详细解决方案

Connections between cities(HDU2874,LCA)

热度:39   发布时间:2023-12-06 14:10:00.0

HDU2874题解

这个题目被网上题解坑惨了,一堆说要用并查集,超时我半天,后来一想,我用的LCA求根的时候不是都搜索了,还用什么并查集,直接搜索的时候给标记就完事了。结果还是T,最后加了读入挂才过,可能是我写的太烂了吧。。。
第二天更新:其实是HDU的锅,第二天不管咋写都能过。

Connections between cities

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K >(Java/Others)
Total Submission(s): 18391 Accepted Submission(s): 4143

Problem Description

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

Sample Output

Not connected
6

Hint

Huge input, scanf recommended.

Source

HDU2874

分析:

题意就是给一个森林,求两点间的最短路,如果没有路就输出一段话。这个就是一个LCA的板子题,就是森林和没路的要稍微处理一下。

参考代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
const int MAXN = 10005;
const int DEG = 20;
using namespace std;int n,m,c,u,v,w,cnt;
int pre[MAXN];
int flag[MAXN];// 读入挂,没开就T
inline void in(int &x){
    x=0;char c=getchar();while(c<48 || c>57) c=getchar();while(c>=48 && c<=57){
    x = x*10+c-48;c = getchar();}
}// 以下是Kuangbin模板修改版
struct Edge {
    int to,next,w;
}edge[MAXN*2];
int head[MAXN],tot;
void addedge(int u,int v,int w) {
    edge[tot].to = v;edge[tot].w = w;edge[tot].next = head[u];head[u] = tot++;
}void init() {
    tot = 0; cnt = 0;for (int i=0;i<=n;i++) {
    pre[i]=i;flag[i]=0;head[i]=-1;}
}
int fa[MAXN][DEG],cost[MAXN][DEG];
int deg[MAXN];void BFS(int root) {
    queue<int>que;deg[root] = 0;fa[root][0] = root;que.push(root);while(!que.empty()) {
    int tmp = que.front();// 搜索的时候标记为所属于的树的编号flag[tmp] = cnt;que.pop();for (int i = 1;i < DEG;i++) {
    fa[tmp][i] = fa[fa[tmp][i-1]][i-1];cost[tmp][i] = cost[fa[tmp][i-1]][i-1] + cost[tmp][i-1];}for (int i = head[tmp];i != -1;i = edge[i].next) {
    int v = edge[i].to;if (v == fa[tmp][0]) continue;cost[v][0] = edge[i].w;deg[v] = deg[tmp] + 1;fa[v][0] = tmp;que.push(v);}}
}int LCA(int u,int v) {
    if (deg[u] > deg[v]) swap(u,v);int hu = deg[u], hv = deg[v];int tu = u, tv = v, ans = 0;for (int det = hv - hu,i = 0;det;det >>= 1,i++) {
    if (det&1) {
    ans += cost[tv][i];tv = fa[tv][i];}}if (tu == tv) return ans;for (int i = DEG-1;i>=0 && tu!=tv;i--) {
    if (fa[tu][i] == fa[tv][i])continue;ans += cost[tu][i] + cost[tv][i];tu = fa[tu][i];tv = fa[tv][i];}ans += cost[tu][0] + cost[tv][0];return ans;
}int main() {
    while(scanf("%d%d%d",&n,&m,&c)!=EOF) {
    init();for (int i=1;i<=m;i++) {
    //scanf("%d%d%d",&u,&v,&w);in(u); in(v); in(w);addedge(u,v,w);addedge(v,u,w);}for (int i=1;i<=n;i++) {
    if (flag[i]==0) {
    // 更新树的数量cnt++;BFS(i);}}for (int i=1;i<=c;i++) {
    //scanf("%d%d",&u,&v);in(u); in(v);// 不属于同一棵树if (flag[u] != flag[v])printf("Not connected\n");elseprintf("%d\n",LCA(u,v));}}return 0;
}
  相关解决方案