HDU2874 Connections between cities(tarjan-lca)

来源:互联网 发布:陈志武的课怎么样 知乎 编辑:程序博客网 时间:2024/05/17 09:16

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10005    Accepted Submission(s): 2400


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 21 3 22 4 35 2 31 44 5
 

Sample Output
Not connected6
Hint
HintHuge input, scanf recommended.
 
#include <bits/stdc++.h>#define maxn 10010using namespace std;int vis[maxn], head1[maxn], head2[maxn];int pre[maxn], dist[maxn], ans[maxn*100];struct node{    int to, next, cos;}G[maxn*2];struct qust{    int to, next, id;}Q[maxn*200];int n, m, c, tot, qoq;void addEdge(int u, int v, int c){    G[tot].to = v, G[tot].cos = c, G[tot].next = head1[u], head1[u] = tot++;    G[tot].to = u, G[tot].cos = c, G[tot].next = head1[v], head1[v] = tot++;}void addQuery(int u, int v, int id){    Q[qoq].to = v, Q[qoq].id = id, Q[qoq].next = head2[u], head2[u] = qoq++;    Q[qoq].to = u, Q[qoq].id = id, Q[qoq].next = head2[v], head2[v] = qoq++;}void init(){    tot = qoq = 0;    for(int i = 1;i <= n;i++){        pre[i] = i;        dist[i] = 0;        vis[i] = 0;        head1[i] = -1;        head2[i] = -1;    }}int Find(int x){    if(pre[x]==x) return x;    return pre[x]=Find(pre[x]);}void tarjan(int cur, int val, int id){    //pre[cur] = cur;    vis[cur] = id;    dist[cur] = val;    for(int i = head1[cur];i != -1;i = G[i].next){        int v = G[i].to;        if(vis[v]) continue;        tarjan(v, val+G[i].cos, id);        pre[v] = cur;    }    for(int i = head2[cur];i != -1;i = Q[i].next){        int v = Q[i].to;        if(vis[v]!=id) continue;        ans[Q[i].id] = dist[cur] + dist[v] - 2*dist[Find(v)];    }}int main(){    int i, a, b, d;    while(~scanf("%d %d %d", &n, &m, &c)){        init();        for(i = 0;i < m;i++){            scanf("%d %d %d", &a, &b, &d);            addEdge(a, b, d);        }        for(i = 0;i < c;i++){            scanf("%d %d", &a, &b);            addQuery(a, b, i);            ans[i] = -1;        }        for(i = 1;i <= n;i++){            if(!vis[i]){                tarjan(i, 0, i);            }        }        for(i = 0;i < c;i++){            if(ans[i]==-1) printf("Not connected\n");            else printf("%d\n", ans[i]);        }    }}


0 0
原创粉丝点击