HDU2874

Posted ---学习ing---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2874相关的知识,希望对你有一定的参考价值。

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.
 

 

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.

 

 

#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdio>
#include <memory.h>
#include<vector>
using namespace std;
int Laxt[10001],Next[20001],To[20001],Len[20001];
int Laxt2[10001],Next2[2000001],To2[2000001],ans[1000001];
bool vis[10001];
int cnt,cnt2;
int dis[10001],fa[10001];
void _update()
{
    memset(Laxt,-1,sizeof(Laxt));
    memset(Laxt2,-1,sizeof(Laxt2));
    memset(vis,false,sizeof(vis));
    cnt=cnt2=0;
}
void _add(int u,int v,int d){
    Next[cnt]=Laxt[u];
    Laxt[u]=cnt;
    To[cnt]=v;
    Len[cnt++]=d;
}
void _add2(int u,int v){
    Next2[cnt2]=Laxt2[u];
    Laxt2[u]=cnt2;
    To2[cnt2++]=v;
    Next2[cnt2]=Laxt2[v];
    Laxt2[v]=cnt2;
    To2[cnt2++]=u;
}
int _findfa(int v){
    if(v==fa[v]) return fa[v];
    return fa[v]=_findfa(fa[v]);
}
void _tarjan(int v)
{
    vis[v]=true;fa[v]=v;
    for(int i=Laxt[v];i!=-1;i=Next[i]){
        if(!vis[To[i]]){
            dis[To[i]]=dis[v]+Len[i];
            _tarjan(To[i]);
            fa[To[i]]=v;
        }
    }
    for(int i=Laxt2[v];i!=-1;i=Next2[i]){
        if(vis[To2[i]]){
            int tmp=_findfa(To2[i]);
            if(dis[To2[i]]!=-1)
            ans[i/2]=dis[v]+dis[To2[i]]-2*dis[tmp];
            else ans[i/2]=-1; 
        }
    }
}
int main()
{ 
    int n,m,c,i,x,y,z;
      while(~scanf("%d %d %d",&n,&m,&c)){
           _update();
           for(i=0;i<m;i++){
                scanf("%d%d%d",&x,&y,&z);
                _add(x,y,z);
                _add(y,x,z);
           }
           for(i=0;i<c;i++){
                scanf("%d%d",&x,&y);
                _add2(x,y);
           }
            for(i=1;i<=n;i++){
                if(!vis[i]){
                  memset(dis,-1,sizeof(dis));
                  dis[i]=0;
                  _tarjan(i);
                }
             } 
               for(i=0;i<c;i++)
               if(ans[i]==-1) printf("Not connected\\n");
               else  printf("%d\\n",ans[i]);
      }
      return 0;
}

 

以上是关于HDU2874的主要内容,如果未能解决你的问题,请参考以下文章

!HDU2874 LCA

HDU2874

HDU - 2874 Connections between cities (LCA)

HDU——2874 Connections between cities

hdu2874(lca / tarjan离线 + RMQ在线)

HDU 2874 Connections between cities(LCA离线算法实现)