HDU 2874 Connections between cities LCA Tarjan 链式前向星

47 篇文章 0 订阅
4 篇文章 0 订阅
Connections between cities
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10011    Accepted Submission(s): 2402


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.




Source
2009 Multi-University Training Contest 8 - Host by BJNU


Recommend
gaojie   |   We have carefully selected several similar problems for you:  2873 2876 2872 2875 2877 

想了好久都没思路….直到后来又读了一次题 才看到那句there might be no path between two cities, no circle exists as well.
求LCA 但是图是一个森林 那就把标记tarjan的visited数组改一下
如果是在当前运行lca的树上的点 就标记为2 其他树的访问过的 标记为1 未访问0
因为需要计算从根到每个节点距离 tarjan完了 dfs计算节点到根的距离的时候顺手标记为1就是了
后来 发现 无限MLE…vector建表改链式前向星就过了 第一次发现链式前向星是那么好…..从此路转粉啊…..

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N=10000+5;
const int M=10000+5;
const int Q=1000000+5;


struct Edge{
    int to,w,next;
};
Edge edge[2*M];
int head[N];

struct Query{
    int to,lca,next;
};
int headQ[N];
Query query[2*Q];

int par[N];//并查集
int visited[N];//Tarjan标记是否被访问过
int dis[N];

inline void addEdge(int k,int u,int v,int w){
    edge[k]={v,w,head[u]};
    head[u]=k;
}

inline void addQuery(int k,int u,int v){
    query[k]={v,-1,headQ[u]};
    headQ[u]=k;
}

inline int find(int x){
    return x==par[x]?x:par[x]=find(par[x]);
}

void Tarjan(int u){
    visited[u]=2;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int to=edge[i].to;
        if(visited[to]==0){
            Tarjan(to);
            par[to]=u;
        }
    }
    for(int i=headQ[u];i!=-1;i=query[i].next){
        int to=query[i].to;
        if(visited[to]==2){
            query[i].lca=find(to);
            query[i^1].lca=query[i].lca;
        }
    }
}

void dfs(int u,int father){
    visited[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int to=edge[i].to;
        if(to==father)
            continue;
        int w=edge[i].w;
        dis[to]=dis[u]+w;
        dfs(to,u);
    }
}

inline int distance(int u,int v,int lca){
    return dis[u]+dis[v]-2*dis[lca];
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n,m,c,u,v,w;
    while(~scanf("%d%d%d",&n,&m,&c)){
        fill(head,head+n+1,-1);
        for(int i=0;i<m;++i){
            scanf("%d%d%d",&u,&v,&w);
            addEdge(2*i,u,v,w);
            addEdge(2*i+1,v,u,w);
        }
        fill(headQ,headQ+n+1,-1);
        for(int i=0;i<c;++i){
            scanf("%d%d",&u,&v);
            addQuery(2*i,u,v);
            addQuery(2*i+1,v,u);
        }
        fill(visited,visited+n+1,0);
        for(int i=1;i<=n;++i)
            par[i]=i;
        for(int i=1;i<=n;++i){
            if(!visited[i]){
                Tarjan(i);
                dis[i]=0;
                dfs(i,-1);
            }
        }
        for(int i=0;i<c;++i){
            if(query[2*i].lca!=-1)
                printf("%d\n",distance(query[2*i+1].to,query[2*i].to,query[2*i].lca));
            else
                puts("Not connected");
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值