Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9053 Accepted Submission(s): 2183
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.
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
的阿德
题意:给出一个森林,有 k 次询问,每次给出两个点 a,b,求a,b间距离,若 a , b 不在一棵树上输出 Not connected
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;
int n, m, q;
int tot;
int f[N];
int fa[N][20], dis[N], dep[N], head[N];
struct xx{
int to, len, nex;
} a[N<<1];
void Init(){
tot = 0;
for(int i = 0; i <= n; i++) f[i] = i;
memset(head , -1, sizeof head);
memset(fa, 0, sizeof 0);
memset(dis, 0, sizeof dis);
memset(dep, 0, sizeof dep);
memset(a, 0, sizeof a);
}
void Add(int u, int v, int w){
a[++tot].to = v, a[tot].len = w;
a[tot].nex = head[u], head[u] = tot;
}
int Find(int x){
return x == f[x] ? x : f[x] = Find(f[x]);
}
void Merge(int a, int b){
int aa = Find(a), bb = Find(b);
if(aa != bb) f[aa] = bb;
}
void dfs(int x, int F, int Dis){
fa[x][0] = F;
dep[x] = dep[F] + 1;
dis[x] = Dis;
for(int i = 1; i <= 19; i++){
fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
for(int i = head[x]; i != -1; i = a[i].nex){
int v = a[i].to;
if(v == F)continue;
dfs(v, x, dis[x] + a[i].len);
}
}
int Lca(int u, int v){
if(dep[u] > dep[v]) swap(u, v);
for(int i = 19; i >= 0; i--){
if(dep[fa[v][i]] >= dep[u]) v = fa[v][i];
}
if(u == v) return u;
for(int i = 19; i >= 0; i--){
if(fa[u][i] != fa[v][i]){
u = fa[u][i], v = fa[v][i];
}
}
return fa[u][0];
}
int main(){
while(scanf("%d%d%d", &n, &m, &q) == 3){
int u, v, w;
Init();
for(int i = 0; i < m; i++){
scanf("%d%d%d", &u, &v, &w);
Add(u, v, w); Add(v, u, w);
Merge(u, v);
}
for(int i = 1; i <= n; i++){
if(f[i] == i) dfs(i, 0, 0);
}
while(q--){
scanf("%d%d", &u, &v);
if(Find(u) == Find(v)) printf("%d\n", dis[u]+dis[v]-2*dis[Lca(u, v)]);
else printf("Not connected\n");
}
}
}
本文探讨了在大规模战争破坏后的城市重建问题,特别是在不同城市间运输关键物资所需的路径规划算法。通过构建森林数据结构,文章解决了给定两座城市间最短路径的问题,并能够判断两城市是否连接在同一棵树中。
489

被折叠的 条评论
为什么被折叠?



