Travel |
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 168 Accepted Submission(s): 74 |
Problem Description
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always take the shortest way. There are M (M <= 3000) two-way roads connecting the towns, and the length of the road is 1.
Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed. |
Input
The input contains several test cases.
The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input. The input will be terminated by EOF. |
Output
Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
|
Sample Input
5 4 5 1 1 3 3 2 5 4 2 2 1 2 1 2 |
Sample Output
INF INF INF INF 2 2
题意: 给你 N 个点 M 条边, 输入M条边,然后 按照输入M条边的顺序,把第i条边删掉,然后求sum,如果不存在sum则输出INF;
首先肯定不能每次都把sum求一遍,复杂度太高 O(n*n*m)。
所以先用BFS预处理一下求最短路,为啥可以用BFS呢,因为权值都为1,所以用BFS就可以了(逃
然后考虑特殊情况 如果有两个结点间有两条边 则可以直接输出;
|
Travel (BFS + 预处理优化)
最新推荐文章于 2024-06-29 10:49:43 发布