Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4425 Accepted Submission(s): 1263
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
题意:给你n给点,m条边,有c次询问,每次询问u,v两个点,你需要判断u,v是否连通,u,v的最短距离是多少。
::对于是否连通直接用并查集就可以了,对于连通的两个点最短距离怎么求呢。
数据很大,不允许每次询问都跑一次最短路的算法,那么就考虑一下一劳永逸的办法,有没有办法经过预处理,每次询问都能快速给出答案。
注意到这是无欢图,那没就转化成树的做法。
对于单个连通分量随意取一点,令其为根,进行dfs遍历,得到每个点到根结点的距离,保存起来(我这里用dp保存),并且得到一个dfs遍历的寻列,求出每两个点的lca。如何求lca具体看
lca –> rmq. 那么对于询问在同一个连通分量的两个的距离dis(u,v) = dp(u)+dp(v)-2*dp(lca(u,v));//u到根结点的距离+v到根结点的距离-(u,v)最早公共祖先到根结点的距离的2倍
view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m, c, fa[N], pre[N], id[N], now, dp[N];
int E[N<<1], pos, R[N], d[N<<1][15];
bool vis[N];
struct edge
{
int u, v, w, p;
edge() {}
edge(int u, int v,int w, int p):u(u), v(v), w(w), p(p) {}
}e[N<<1];
int ecnt;
int find(int x)
{
return x==fa[x]?x:(fa[x]=find(fa[x]));
}
void init_RMQ()
{
for(int i=0; i<pos; i++) d[i][0] = E[i];
for(int j=1; (1<<j)<pos; j++)
for(int i=0; i+(1<<j)-1<pos; i++)
d[i][j] = min(d[i][j-1], d[i+(1<<(j-1))][j-1]);
}
int RMQ(int L, int R)
{
int k=0;
while((1<<(k+1)) <= R-L+1) k++;
return min(d[L][k], d[R-(1<<k)+1][k]);
}
void dfs(int u, int h)
{
vis[u] = 1;
id[u] = now++;//给每个结点一个新的id,为什么要给新的id,为什么不用原来的序号,弄懂求lca就懂了
R[id[u]] = pos;
E[pos++] = id[u];
dp[id[u]] = h;//结点id[u]到根结点的距离为h
for(int i=pre[u]; ~i; i=e[i].p)
{
int v = e[i].v;
if(vis[v]) continue;
dfs(v, h+e[i].w);
E[pos++] = id[u];
}
}
void init()
{
memset(vis, 0, sizeof(vis));
now = 1; pos = 0;
for(int i=1; i<=n; i++) if(!vis[i]) dfs(i, 0);
// for(int i=1; i<=n; i++) printf("is[%d] = %d, R=%d\n", i, id[i], R[id[i]]);
// for(int i=0; i<pos; i++) printf("pos[%d] = %d\n", i, E[i]);
init_RMQ();
}
void solve()
{
for(int i=1; i<=n; i++) fa[i] = i, pre[i] = -1;
ecnt = 0;
int u, v, w;
for(int i=0; i<m; i++)
{
scanf("%d%d%d", &u, &v, &w);
e[ecnt] = edge(u, v, w, pre[u]);
pre[u] = ecnt++;
e[ecnt] = edge(v, u, w, pre[v]);
pre[v] = ecnt++;
u =find(u), v =find(v);
fa[u] = v;
}
init();
while(c--)
{
scanf("%d%d", &u, &v);
if(find(u)!=find(v)){
puts("Not connected");
continue;
}
u = id[u], v = id[v];
int lca = RMQ(min(R[u], R[v]), max(R[u],R[v]));
int ans = dp[u]+dp[v]-2*dp[lca];
printf("%d\n", ans);
}
}
int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d%d%d", &n, &m, &c)>0) solve();
return 0;
}