Connections between cities(LCA+并查集)

该博客讨论了一道编程竞赛题目,要求在受损城市间重建道路并找出任意两城市间的最短路径。题目描述了输入格式和输出要求,并给出样例输入和输出。解决方案涉及利用LCA(最近公共祖先)算法和并查集来判断城市是否在同一连通分量中,以及计算最短路径。作者提到在实现过程中遇到的问题,即需要将最大城市数量的限制扩大两倍才能正确运行代码。
摘要由CSDN通过智能技术生成

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2874

Connections between cities

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

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 conne

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值