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条边,然后就是查询c次,查询两个顶点之间是否联通,如果联通的话,问你这两个顶点之间最小的距离是多少,如果不连通的话就输出-1.
思路:对每一个子图跑一次tarjin。因为他有可能存在多个子树的情况。所以对没有遍历过的顶点跑一次tarjin。然后lca数组任然为-1的话就是不联通。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e4+7;
const int maxc=1e6+7;
struct edge
{
int to,next,w;
}e[maxn<<1];
struct query
{
int to,next;
}q[maxc<<1];
int lca[maxc];
int head[maxn],first[maxn],cnt,tot;
int par[maxn],n,d[maxn];
bool vis[maxn],V[maxn];
void init()
{
for(int i=0;i<=n;i++) d[i]=0;
for(int i=0;i<=n;i++) vis[i]=false;
for(int i=0;i<=n;i++) V[i]=false;
for(int i=0;i<=n;i++) par[i]=i;
for(int i=0;i<=n;i++) head[i]=-1;
cnt=-1;
for(int i=0;i<=n;i++) first[i]=-1;
tot=-1;
}
void add_edge(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
void add_query(int u,int v)
{
q[++tot].to=v;
q[tot].next=first[u];
first[u]=tot;
}
int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
}
void unit(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy) return ;
par[fy]=fx;
}
void tarjan(int u)
{
vis[u]=true;
V[u]=true;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(vis[v]) continue;
d[v]=d[u]+e[i].w;
tarjan(v);
unit(u,v);
}
for(int i=first[u];i!=-1;i=q[i].next)
{
int v=q[i].to;
if(!vis[v]) continue;
lca[i>>1]=d[u]+d[v]-(d[find(v)]*2);
}
}
int main()
{
int m,c;
while(~scanf("%d%d%d",&n,&m,&c))
{
init();
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
for(int i=0;i<c;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_query(u,v);
add_query(v,u);
}
for(int i=0;i<maxc;i++) lca[i]=-1;
for(int i=1;i<=n;i++)
{
if(!V[i])
{
memset(vis,0,sizeof(vis));
tarjan(i);
}
}
for(int i=0;i<c;i++)
{
if(lca[i]==-1)
printf("Not connected\n");
else
printf("%d\n",lca[i]);
}
}
return 0;
}