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.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874
求森林的lca,在lca模板的基础上加一个判断是否在同一颗树即可。
注意,这个题用tarjan离线看内存,不要记录无用的东西.
#pragma GCC optimize(2) #include<stdio.h> #include<algorithm> #include<string.h> #include<queue> using namespace std; const int maxn = 1e4 + 100; const int inf = 0x3f3f3f3f; typedef long long ll; int t, n, m, tt, tot; struct node //不需要记录起点了,也不需要写起点的变量了,不然内存卡死 { int v, w, next; }edge[maxn * 2]; struct qre { int t, next, index; }query[maxn * 200]; int head[maxn], h[maxn], dis[maxn], vis[maxn], ans[maxn*100], arc[maxn], mark[maxn]; void init() { memset(head, -1, sizeof(head)); memset(h, -1, sizeof(h)); memset(vis, false, sizeof(vis)); memset(ans, -1, sizeof(ans)); memset(dis, 0, sizeof(dis)); memset(mark, 0, sizeof(mark)); tt = tot = 0; return; } void addedge(int u, int v, int w) //不需要记录起点了 { edge[tot].v = v; edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot++; edge[tot].v = u; edge[tot].w = w; edge[tot].next = head[v]; head[v] = tot++; return; } void addedge1(int s, int t, int index) { query[tt].t = t; query[tt].index = index; query[tt].next = h[s]; h[s] = tt++; query[tt].t = s; query[tt].index = index; query[tt].next = h[t]; h[t] = tt++; return; } int find(int x) { if (x == arc[x]) { return x; } else { return arc[x] = find(arc[x]); } } void tarjan(int s, int fa) { arc[s] = s; vis[s] = true; for (int i = head[s]; i != -1; i = edge[i].next) { int v = edge[i].v; int w = edge[i].w; if (v == fa) { continue; } dis[v] = dis[s] + w; tarjan(v, s); arc[v] = s; } for (int j = h[s]; j != -1; j = query[j].next) { int v = query[j].t; int d = query[j].index; if (vis[v] && !mark[find(v)] && ans[d] == -1) { int t = find(v); ans[query[j].index] = dis[s] + dis[v] - 2 * dis[t]; } } } int main() { //freopen("C://input.txt", "r", stdin); while (scanf("%d%d%d", &t, &n, &m) != EOF) { init(); for (int i = 1; i <= n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); } for (int i = 1; i <= m; i++) { int s, e; scanf("%d%d", &s, &e); addedge1(s, e, i); } for (int i = 1; i <= n; i++) { if (!vis[i]) { tarjan(i, 0); mark[i] = 1; //记录是否在同一颗树的 } } for (int i = 1; i <= m; i++) { if (ans[i] == -1) { puts("Not connected"); } else printf("%d\n", ans[i]); } } return 0; }
hdu 2874 Connections between cities (tarjan离线 卡内存)
最新推荐文章于 2022-11-26 11:07:01 发布