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
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.
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 connected6HintHintHuge 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
- hdu2874-Connections between cities (LCA/离线tarjan)
- HDU2874 Connections between cities(tarjan-lca)
- hdu2874 Connections between cities--LCA
- HDU2874 Connections between cities【LCA】
- hdu2874 Connections between cities(LCA)
- hdu2874 Connections between cities - LCA在线算法
- 【HDU2874】Connections between cities-LCA算法
- hdu2874 Connections between cities (LCA离线)
- hdu2874—Connections between cities(LCA)
- hdu2874 Connections between cities
- HDU2874 Connections between cities
- hdu2874 Connections between cities
- HDU2874 Connections between cities LCA+DFS或并查集
- hdu2874 Connections between cities(LCA离线+并查集)
- hdoj 2874 Connections between cities 【Tarjan离线LCA】
- HDU 2874 - Connections between cities(LCA‘离线算法Tarjan)
- HDU 2874 Connections between cities(LCA Tarjan)
- HDU2874——Connections between cities 详解 (LCA,RMQ,数据结构,dfs序,并查集)
- bzoj 1015: [JSOI2008]星球大战starwar
- 51nod 1186 质数检测 V2
- HDOJ 5944 Fxx and string
- 大数据从业方向及从业方向具备的技能
- Java String.split()函数的用法和正则表达式
- HDU2874 Connections between cities(tarjan-lca)
- HTTP/2.0 相比1.0有哪些重大改进?
- 51nod 1191 消灭兔子
- 在ViewPager中使用WebView时的问题
- 算法 排序算法之简单选择排序
- Vine已死,秒拍抱团崛起,同是视频社交差距在哪?
- 51nod 1202 子序列个数
- poj 1753 Flip Game 搜索
- 51nod 1212 无向图最小生成树