题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874
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 6HintHint Huge input, scanf recommended.
Source
题意,给你n个点,m条边,c个查询,求两点的最小距离,由于n和m和c都很大,所以用LCA离线算法。。
代码:
#include<stdio.h>
#include<string.h>
const int maxn=20010;
struct node
{
int from;
int to;
int lca;
int next;
}edge[maxn],qedge[maxn*100];
int head[maxn],qhead[maxn];
int vis[maxn],dist[maxn];
int p[maxn];
int n,m,c;
int cnt1=0,cnt2=0;
void add1(int x,int y,int z)
{
edge[cnt1].from=x;
edge[cnt1].to=y;
edge[cnt1].lca=z;
edge[cnt1].next=head[x];
head[x]=cnt1++;
}
void add2(int x,int y)
{
qedge[cnt2].from=x;
qedge[cnt2].to=y;
qedge[cnt2].lca=-1;
qedge[cnt2].next=qhead[x];
qhead[x]=cnt2++;
}
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
void LCA(int u)
{
p[u] = u;
vis[u] = 1;
for(int k=head[u];k!=-1;k=edge[k].next)
{
if(!vis[edge[k].to])
{
dist[edge[k].to] = dist[u] + edge[k].lca;
LCA(edge[k].to);
p[edge[k].to] = u;
}
}
for(int k=qhead[u];k!=-1;k=qedge[k].next)
{
int x = qedge[k].to;
if(vis[x] == 1)
{
int t = find(x);
qedge[k].lca = dist[x] - 2*dist[t] + dist[u];
qedge[k^1].lca = qedge[k].lca;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d %d",&n,&m,&c)!=EOF)
{
cnt1=0;
cnt2=0;
memset(head,-1,sizeof(head));
memset(qhead,-1,sizeof(qhead));
memset(vis,0,sizeof(vis));
memset(p,-1,sizeof(p));
memset(dist,0,sizeof(dist));
int x,y,z;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&z);
add1(x,y,z);
add1(y,x,z);
}
for(int i=1;i<=c;i++)
{
scanf("%d %d",&x,&y);
add2(x,y);
add2(y,x);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
LCA(i);
}
for(int i=0;i<cnt2;i+=2)
{
int x=qedge[i].from;
int y=qedge[i].to;
if(find(x)!=find(y))
printf("Not connected\n");
else
printf("%d\n",qedge[i].lca);
}
}
return 0;
}