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
换了个姿势还是上了HDU 据学长给我的图论题库来说 lca问题也写完了
这题几乎还是模板题 只不过是多了个 判断是否在同一棵树上 用了点小技巧 再Tarjan时 把根节点也传上去 如果得结果时根节点不同就跳过
另外这题灰常容易爆内存 稍微改了一下用 C++水过去了 然而G++还是爆………………
ac code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn=10005;
const int inf=0x3f3f3f3f;
struct node
{
int v,d;
node* nxt;
};
node *head1[maxn],edge1[maxn*2];
node *head2[maxn],edge2[2000002];
int root[maxn],ans[1000002],dis[maxn];
int vis[maxn];
int n,lnum,qnum,idx1,idx2;
void init()
{
for(int i=0;i<=n;i++)
{
head1[i]=head2[i]=0;
vis[i]=0;
}
idx1=idx2=0;
}
int fin(int x)
{
if(root[x]==x) return x;
return root[x]=fin(root[x]);
}
void add(int u,int v,int d,node *head[],node edge[],int& idx)
{
edge[idx].v=v;
edge[idx].d=d;
edge[idx].nxt=head[u];
head[u]=edge+idx++;
edge[idx].v=u;
edge[idx].d=d;
edge[idx].nxt=head[v];
head[v]=edge+idx++;
}
void Tarjan(int u,int rot)
{
root[u]=u;
vis[u]=rot;
for(node* p=head2[u];p;p=p->nxt)
if(vis[p->v]==rot)
ans[p->d]=dis[u]+dis[p->v]-2*dis[fin(p->v)];
for(node* p=head1[u];p;p=p->nxt)
if(!vis[p->v])
{
dis[p->v]=dis[u]+p->d;
Tarjan(p->v,rot);
root[p->v]=u;
}
}
int main()
{
int u,v,d;
while(scanf("%d%d%d",&n,&lnum,&qnum)!=EOF)
{
init();
for(int i=1;i<=lnum;i++)
{
scanf("%d %d %d",&u,&v,&d);
add(u,v,d,head1,edge1,idx1);
}
for(int i=1;i<=qnum;i++)
{
scanf("%d %d",&u,&v);
ans[i]=inf;
add(u,v,i,head2,edge2,idx2);
}
for(int i=1;i<=n;i++)
if(!vis[i])
{
dis[i]=0;
Tarjan(i,i);
}
for(int i=1;i<=qnum;i++)
{
if(ans[i]==inf) puts("Not connected");
else printf("%d\n",ans[i]);
}
}
return 0;
}