lca是啥?rmq又是啥?咋又突然变成并查集的东东了咧?不是吧我树状数组还半懂不懂完全不懂啊!
Connections between cities |
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 168 Accepted Submission(s): 52 |
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 |
先科普。
lca:最近公共祖先(Least Common Ancestors) 对于有根树T的两个结点u、v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u、v的祖先且x的深度尽可能大。另一种理解方式是把T理解为一个无向无环图,而LCA(T,u,v)即u到v的最短路上深度最小的点。
rmq:Range Minimal Query 也即对一个区间的最小值询问。对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就是说,RMQ问题是指求区间最值的问题。
科普完赫然发现最简单的代码就是普通的并查集……而且还比较快,就是空间小……4倍的代码量换10倍的空间,划算否?anyway,acm超空间的几率比较低,所以……就这样吧>_<~~得过且过是我的座右铭谢谢。