暑期集训4 K题
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2874
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
题意:
给出n个点以及他们之间的一些距离,保证n个点不会连成环,先进行k次询问,每次给出两个点a,b,求a,b间距离
题解:
对于是否在一个树上可以用并查集
在一个树上的用lca求公共祖先即可
第一次写lca..写的好难看
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
int to,l,next;
}dmap[80005];
int father[20005];
int head[20005];
int xu[80005];
int mi[20];
int deep[20005];
int l[20005];
int dp[20005][20];
int lon[20005];
int drank[20005];
int mark[20005];
int nn,mm,mn;
int findroot(int a)
{
if(father[a]!=a) father[a]=findroot(father[a]);
return father[a];
}
void unionset(int a,int b)
{
int fa=findroot(a);
int fb=findroot(b);
if(fa>fb) father[fa]=fb;
else if(fa<fb) father[fb]=fa;
}
void add(int a,int b,int c)
{
dmap[nn].to=b;
dmap[nn].l=c;
dmap[nn].next=head[a];
head[a]=nn++;
}
void dfs(int n,int ndeep)
{
int i;
drank[mn]=n;
mark[n]=mn;
xu[mm++]=mn;
mn++;
l[n]=mm-1;
for(i=head[n];1;i=dmap[i].next)
{
if(i==-1) break;
if(deep[dmap[i].to]==ndeep-1) continue;
deep[dmap[i].to]=ndeep+1;
lon[dmap[i].to]=lon[n]+dmap[i].l;
dfs(dmap[i].to,ndeep+1);
xu[mm++]=mark[n];
}
}
void init()
{
int i,j;
mi[0]=-1;
for(i=1;i<=nn;i++)
{
int cc=i&(i-1);
if(cc==0) mi[i]=mi[i-1]+1;
else mi[i]=mi[i-1];
dp[i][0]=xu[i];
}
for(j=1;j<=mi[nn];j++)
for(i=1;i+(1<<j)-1<=nn;i++)
dp[i][j]=min(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
}
int rmq(int x,int y)
{
int kk=mi[y-x+1];
return min(dp[x][kk],dp[y-(1<<kk)+1][kk]);
}
int lca(int a,int b)
{
if(l[a]>l[b]) return drank[rmq(l[b],l[a])];
else return drank[rmq(l[a],l[b])];
}
int main()
{
int n,m,k,i,a,b,c;
while(~scanf("%d%d%d",&n,&m,&k))
{
nn=0;
mm=1;
mn=1;
memset(dmap,0,sizeof(dmap));
for(i=0;i<=n;i++)
{
father[i]=i;
head[i]=-1;
deep[i]=-1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
unionset(a,b);
add(a,b,c);
add(b,a,c);
}
for(i=1;i<=n;i++)
{
if(father[i]==i)
{
add(0,i,0);
add(i,0,0);
}
}
deep[0]=1;
dfs(0,1);
init();
for(i=1;i<=k;i++)
{
scanf("%d%d",&a,&b);
int a1=findroot(a);
int b1=findroot(b);
if(a1!=b1) printf("Not connected\n");
else
{
int c=lca(a,b);
int ans=lon[a]+lon[b]-2*lon[lca(a,b)];
printf("%d\n",ans);
}
}
}
return 0;
}