HDU 2433 - Travel
来源:互联网 发布:ubuntu 卸载apache2 编辑:程序博客网 时间:2024/06/02 23:47
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.
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.
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 45 11 33 25 42 21 21 2
Sample Output
INFINFINFINF22
题意:给出边建立图 然后依次删除给出的边,求最短路,如无法连通 则返回INF
这里最短路 = 多源最短路和 即求出对于单源最短路 再for一个循环求出多源最短路 相加
用dijkstra 复杂度为V*(E+V),由于这里很特殊 每条路长度都是1 故可以使用bfs 最先搜到的节点一定是最短的
bfs 时间复杂度 E , 但如果每次拆路都对所有节点重新求最短路 复杂度则是 E*V*E 还多个case 可能不过
这里可以使用used[x][u][v] 来记录对于以x为源点的树 是否使用town[u][v] 这条路 若有,则更新sum[x], 若无 continue
即更新所有有用到被拆路的树。。。
感谢:http://www.cppblog.com/keroro/archive/2013/05/27/200622.html?opt=admin
另外:ans = ans + sum_v + sum_u - sum[v] - sum[u]的方法是错误的,只是数据能过而已。
个人感悟:
①不优化的复杂度是O(E*E*V),也是临界可解,以后遇到这样的题目要往优化的方面想。
0 0
- hdu 2433 TRAVEL
- HDU 2433 - Travel
- HDU 2433 Travel
- HDU 2433 Travel
- hdu 2433 Travel 最短路
- hdu 2433 Travel bfs+剪枝
- hdu 2433 Travel(spfa)
- hdu Travel
- hdu-travel
- Travel HDU
- Travel HDU
- Travel HDU
- hdu 2433 Travel 最短路+预处理
- Hdu 2433 Travel 删边+最短路之和
- 【HDU】2433 Travel 最短路树优化
- HDU 2433 Travel 最短路SPAF
- hdu 2433 Travel 最短路 dijkstra算法。
- HDU 2433 Travel 最短路树
- js字符串正则替换
- Apache Storm 学习:Supervisor,Worker,Executor,Task
- Tcpdump 和 Wireshark 的结合使用(一)
- 数字图像处理领域的二十四个典型算法及vc实现、第一章
- Javascript中null和undefined的区别
- HDU 2433 - Travel
- 旭说数据结构之二叉查找树
- sql server T-SQL 基础
- SQLServer:定时作业的设置方法
- 七步写出优秀的JS代码
- 在Android加入和使用Realm
- adb命令、adb shell与Linux各种命令(busybox)
- python argparse的例子
- iOS 网络请求返回错误码汇总