Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6497 Accepted Submission(s): 1693
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
Hint Huge input, scanf recommended.
题意:给你N个点、M条无向边以及边的长度,可能这些点和边构成的不是一棵树,而是森林。然后给出Q次查询,查询两个节点间的最短距离。
提醒:最短路径的算法果断不能用,10000个点、10000条边加上1000000次查询,5s绝对TLE到死。
还好花了半天时间学了LCA 转 RMQ的算法,在此ORZ一下九野大大!
LCA 转 RMQ 算法解析 看这里:
点我
思路:由于可能不是连通图,所以要求出每棵树的根,并设置一个虚拟根节点root连通所有的树。我们用dist[ i ]存储i节点到root的距离。那么对于查询的两点u、v,它们之间距离为 dist[ u ] + dist[ v ] - 2 * dist[ LCA(u,v)]。
AC代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define MAXN 10000+100
#define MAXM 40000+100
#define LL long long
using namespace std;
struct Edge
{
int from, to, val, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int depth[MAXN<<1];//depth[i] 第i次访问节点的深度
int id[MAXN];//id[i] i节点在vs第一次出现的下标
int vs[MAXN<<1];//vs[i] 第i次访问的节点编号
int dfs_clock;//时间戳
int pre[MAXN];//并查集存储父节点
int dist[MAXN];//节点到根节点的距离
int N, M, Q;//N个点 M条边 Q次查询
int dp[MAXN<<1][20];//存储 depth数组里面 以下标i开头的,长度为2^K的区间最小值 所对应的下标
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
for(int i = 1; i <= N; i++)
pre[i] = i;
}
int find(int p)
{
int t;
int child = p;
while(p != pre[p])
p = pre[p];
while(child != p)
{
t = pre[child];
pre[child] = p;
child = t;
}
return p;
}
void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
if(fx < fy)
pre[fx] = fy;
else
pre[fy] = fx;
}
void addEdge(int u, int v, int w)
{
Edge E = {u, v, w, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
void getMap()
{
int a, b, c;
for(int i = 1; i <= M; i++)
{
scanf("%d%d%d", &a, &b, &c);
merge(a, b);
addEdge(a, b, c);
addEdge(b, a, c);
}
}
void DFS(int u, int fa, int d)
{
id[u] = dfs_clock;
vs[dfs_clock] = u;
depth[dfs_clock++] = d;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
dist[v] = dist[u] + edge[i].val;
DFS(v, u, d+1);
vs[dfs_clock] = u;
depth[dfs_clock++] = d;
}
}
void find_depth()
{
dfs_clock = 1;
memset(depth, 0, sizeof(depth));
memset(id, 0, sizeof(id));
memset(vs, 0, sizeof(vs));
memset(dist, 0, sizeof(dist));
DFS(0, -1, 0);
}
void RMQ_init(int NN)//预处理 区间最小值
{
for(int i = 1; i <= NN; i++)
dp[i][0] = i;
for(int j = 1; (1<<j) <= NN; j++)
{
for(int i = 1; i + (1<<j) - 1 <= NN; i++)
{
if(depth[dp[i][j-1]] <= depth[dp[i + (1<<(j-1))][j-1]])
dp[i][j] = dp[i][j-1];
else
dp[i][j] = dp[i + (1<<(j-1))][j-1];//这里少写个括号 RE2小时 位运算真娇贵
}
}
}
int query(int L, int R)
{
//查询L <= i <= R里面使depth[i]最小的点 i
//注意返回的是 数组下标
int k = 0;
while(1<<(k+1) <= R-L+1) k++;
if(depth[dp[L][k]] <= depth[dp[R - (1<<k) + 1][k]])
return dp[L][k];
else
return dp[R - (1<<k) + 1][k];
}
int LCA(int a, int b)//找出vs[a] 和 vs[b]里面的最大值 最小值 求LCA
{
int x, y;
if(id[a] <= id[b])
x = id[a], y = id[b];
else
x = id[b], y = id[a];
return vs[query(x, y)];//返回节点的编号
}
bool Ispoint[MAXN];
void solve()
{
//建立虚拟根0 把森林变成一棵树
for(int i = 1; i <= N; i++)//虚根连接 每棵树的根节点
if(pre[i] == i) addEdge(0, i, 0);
find_depth();//遍历树
RMQ_init(dfs_clock-1);//预处理区间最小值
int a, b;
while(Q--)
{
scanf("%d%d", &a, &b);
if(find(a) != find(b))//不在同一棵树
printf("Not connected\n");
else if(a == b)
printf("0\n");
else
printf("%d\n", dist[a] + dist[b] - 2 * dist[LCA(a, b)]);
}
}
int main()
{
while(scanf("%d%d%d", &N, &M, &Q) != EOF)
{
init();
getMap();
solve();
}
return 0;
}