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.
分析:
第一种很常见了,并查集来维护是否 在一个联通快内,然后求LCA,用公式来求之间的最短距离。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10000+11;
const int M = 10000+11;
struct Edge {
int from,to,val,next;
}edge[M*2];
int head[N],top;
void init(){
memset(head,-1,sizeof(head));
top=0;
}
void addedge(int a,int b,int c){
Edge e={a,b,c,head[a]};
edge[top]=e;head[a]=top++;
}
int dis[N],dep[N]; int fa[20][N];
void dfs(int now,int pr){
fa[0][now]=pr;
for(int i=head[now];i!=-1;i=edge[i].next){
Edge e=edge[i];
if(e.to==pr) continue;
dis[e.to]=dis[now]+e.val;
dep[e.to]=dep[now]+1;
dfs(e.to,now);
}
}
void lca_init(int n){
for(int k=1;k<20;k++){
for(int i=1;i<=n;i++) {
if(fa[k-1][i]==-1) fa[k][i]=-1;
else fa[k][i]=fa[k-1][fa[k-1][i]];
}
}
}
int LCA(int a,int b){
if(dep[a]>dep[b]) swap(a,b);
int d=dep[b]-dep[a];
for(int i=0;i<20;i++)
if((1<<i )& d) b=fa[i][b];
if(a==b) return a;
for(int i=19;i>=0;i--){
if(fa[i][a]!=fa[i][b]){
a=fa[i][a];
b=fa[i][b];
}
}
return fa[0][a];
}
int pre[N];
int Find(int x){
return x==pre[x]?x:(pre[x]=Find(pre[x]));
}
void Join(int a,int b){
a=Find(a);b=Find(b);
if(a!=b) pre[a]=b;
}
int main(){
int n,m,q;
while(~scanf("%d%d%d",&n,&m,&q)){
init();
for(int i=1;i<=n;i++) pre[i]=i;
while(m--){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
Join(a,b);
addedge(a,b,c);
addedge(b,a,c);
}
memset(dis,0,sizeof(dis));
memset(fa,-1,sizeof(fa));
memset(dep,0,sizeof(dep));
for(int i=1;i<=n;i++)
if(!dis[i]) dfs(i,-1);
// for(int i=1;i<=n;i++) printf("%d %d \n",dep[i],dis[i]);
lca_init(n);
while(q--){
int a,b; scanf("%d%d",&a,&b);
if(Find(a)!=Find(b)) puts("Not connected");
else printf("%d\n",-2*dis[LCA(a,b)]+dis[a]+dis[b]);
}
}
return 0;
}
分析二 :接下来才是很关键的,倍增法求lca其实很方便,其一 :代码短 ,思路简单。
其二 : 只要是静态的树,我们可以用倍增法来维护好多东西, 而且都是非常方便,本道题为例,可以维护dis[ k ] [ v ] v 向上走2^k步的距离。
不只是维护 距离这么简单,同时我们还可以维护 mx[ k ] [v ] v向上走2^k步内的边权最大,同理边权最小也是可以维护。实现起来也是很简单 。很推荐倍增法求lca 。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10000+11;
const int M = 10000+11;
struct Edge {
int from,to,val,next;
}edge[M*2];
int head[N],top;
void init(){
memset(head,-1,sizeof(head));
top=0;
}
void addedge(int a,int b,int c){
Edge e={a,b,c,head[a]};
edge[top]=e;head[a]=top++;
}
int dep[N]; int fa[30][N];int dis[20][N];
bool vis[N];
void dfs(int now,int pr,int d){
dep[now]=d; fa[0][now]=pr; vis[now]=1;
for(int i=head[now];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==pr) continue;
dis[0][v]=edge[i].val;
dfs(v,now,d+1);
}
}
void lca_init(int n){
for(int k=1;k<30;k++){
for(int i=1;i<=n;i++) {
if(fa[k-1][i]==-1) fa[k][i]=-1;
else {
fa[k][i]=fa[k-1][fa[k-1][i]];
dis[k][i]=dis[k-1][i]+dis[k-1][fa[k-1][i]];
}
}
}
}
int LCA(int a,int b){ // 求lca的同时维护 距离
if(dep[a]>dep[b]) swap(a,b);
int ans=0;
int d=dep[b]-dep[a];
for(int i=0;i<30;i++)
if((1<<i )& d) {
ans+=dis[i][b];
b=fa[i][b];
}
if(a==b) return ans;
for(int i=29;i>=0;i--){
if(fa[i][a]!=fa[i][b]){
ans+=dis[i][a]; a=fa[i][a];
ans+=dis[i][b]; b=fa[i][b];
}
}
ans+=dis[0][a]; ans+=dis[0][b];
return ans;
}
int pre[N];
int Find(int x){
return x==pre[x]?x:(pre[x]=Find(pre[x]));
}
void Join(int a,int b){
a=Find(a);b=Find(b);
if(a!=b) pre[a]=b;
}
int main(){
int n,m,q;
while(~scanf("%d%d%d",&n,&m,&q)){
init();
for(int i=1;i<=n;i++) pre[i]=i;
while(m--){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
Join(a,b);
addedge(a,b,c);
addedge(b,a,c);
}
memset(dis,0,sizeof(dis));
memset(fa,-1,sizeof(fa));
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
if(!vis[i]) dfs(i,-1,0);
// for(int i=1;i<=n;i++) printf("%d ",dis[0][i]);
lca_init(n);
while(q--){
int a,b;scanf("%d%d",&a,&b);
if(Find(a)!=Find(b)) puts("Not connected");
else printf("%d\n",LCA(a,b));
}
}
return 0;
}