HDU2433—Travel (BFS,最短路)
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3059 Accepted Submission(s): 1051
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
Source
2008 Asia Chengdu Regional Contest Online
Recommend
lcy | We have carefully selected several similar problems for you: 2429 2437 2430 2436 2435
大意:
给定一个图,N个节点,M条双向边。记i节点到其他节点的最短距离的和为Si,求Si(1<=i<=N)的和sum
然后假设其中一个边被毁,求此时的sum
思路:
若使用那几个最短路算法,之后每假设一个边被毁,就需要全部重新计算一遍,时间复杂度很高
注意到每个边的权值都为1,此时用BFS求最短路径的话会很方便
而且,BFS可以很好地解决之后删边的问题:
只要记录一下第一次每个节点的BFS树,之后再判断要删的点是否在树枝上即可
两处优化:
1.原图为非连通图,无论删哪条边,距离恒为INF
2.有重边,删除其中一条对全图无影响(因此在记录图时,记录的是两点之间边的个数)
代码:
- #include <bits/stdc++.h>
- using namespace std;
- typedef struct Edge
- {
- int beg, end;
- } Edge;
- Edge edge[3001];
- int Sum[101] = {0};
- int Map[101][101];
- bool visited[101];
- int prec[101][101]; // prec[x][y],用来存储第一遍第x个节点的BFS树中y节点的上一个节点
- int N, M;
- int BFS(int k, bool is_preprocess = true)//第二个参数表示的是否是第一遍遍历所有节点
- {
- int dis[101] = {0}; //记录到根节点的距离
- memset(visited, false, sizeof(visited));
- queue<int> Q;
- visited[k] = true;
- Q.push(k);
- int cnt = 1;
- int Sum = 0;
- while (!Q.empty())
- {
- int i = Q.front(); Q.pop();
- for (int j = 1; j <= N; ++j)
- {
- if (!visited[j] && Map[i][j] > 0)
- {
- visited[j] = true;
- if (is_preprocess)
- prec[k][j] = i; //记录前驱,只记录第一次遍历的BFS树的节点前驱,由于BFS时要对每个节点都进行一遍BFS所以需要记录n个BFS树
- dis[j] = dis[i] + 1; //记录到根节点的距离
- Sum += dis[j];
- ++cnt; //记录一下BFS中的节点数目,最后判断是否能够到达所有节点(cnt==N)
- Q.push(j);
- }
- }
- }
- return (cnt == N) ? Sum : -1;
- }
- int main()
- {
- int x, y, sum ;
- while (cin >> N >> M)
- {
- memset(Sum, 0, sizeof(Sum));
- memset(Map, 0, sizeof(Map));
- memset(prec, 0, sizeof(prec));
- int i = 1;
- for ( ; i <= M; ++i)
- {
- //建树
- cin >> x >> y;
- Map[x][y]++;
- Map[y][x]++;//这样记录路径的话,可以方便的记录重边
- edge[i].beg = x;
- edge[i].end = y;
- }
- sum = 0;
- i = 1;
- for (; i <= N; ++i)
- {
- Sum[i] = BFS(i); //需要存储每个节点的BFS树的路径和
- if (Sum[i] == -1)
- break;
- else
- sum += Sum[i];
- }
- if (i <= N) //优化:原图为非连通图,无论删哪条边,距离恒为INF
- {
- for (int i = 0; i < M; ++i)
- puts("INF");
- continue;
- }
- for (int i = 1; i <= M; ++i)
- {
- int x = edge[i].beg;
- int y = edge[i].end;
- if (Map[x][y] > 1) //优化:有重边,删除其中一条对全图无影响
- {
- cout << sum << endl;
- }
- else //删的是割边(不可能有Map[][]==0,因为前边过滤了非连通图)
- {
- int sum1 = 0, j = 1, s1;
- for (; j <= N; ++j)//遍历全部顶点,蛮力法
- {
- if (prec[j][y] == x || prec[j][x] == y) //x-y在第j棵bfs树上,只能蛮力
- {
- Map[x][y] = Map[y][x] = 0; //删边
- s1 = BFS(j, false);
- if (s1 == -1) //非连通,直接返回INF
- {
- Map[x][y] = Map[y][x] = 1; //恢复边
- break; //跳转到114行
- }
- sum1 += s1;
- Map[x][y] = Map[y][x] = 1; //恢复边
- }
- else
- {
- sum1 += Sum[j];
- }
- }
- if (j <= N)
- {
- puts("INF");
- // continue;
- }
- else
- {
- cout << sum1 << endl;
- }
- }
- }
- }
- return 0;
- }
HDU2433—Travel (BFS,最短路)的更多相关文章
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...
- BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- HDU2433 BFS最短路
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 2019.01.22 SCU4444 Travel(最短路+bfs)
传送门 题意简述:给出一张nnn个点的完全图,有mmm条边边权为aaa其余点边权为bbb,问从111到nnn的最短路. 思路:分类讨论一波即可. (1,n)(1,n)(1,n)的边权为aaa,那么只用 ...
- HDU 2433 Travel (最短路,BFS,变形)
题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...
- Full Tank? POJ - 3635 (bfs | 最短路)
After going through the receipts from your car trip through Europe this summer, you realised that th ...
随机推荐
- F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意: 查找区间k的后继. 思路: 直接主席树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio&g ...
- MySQL的事务和锁
MySQL的事务和锁 阅读目录 什么是事务 事务:是数据库操作的最小工作单元,是作为单个逻辑工作单元执行的一系列操作:这些操作作为一个整体一起向系统提交,要么都执行.要么都不执行:事务是一组不可再 ...
- 学习python基础规则
前面应该是记流水账的方式,毕竟学习的内容不多无法产出什么有效的内容. 这两天从开始下载Python开始学习,一路顺畅冒的问题,直到开始学习python的游戏规则,严格缩进.注释及‘’的使用等感觉还不错 ...
- copy小练习
# 1. # 有如下 # v1 = {'郭宝元', '李杰', '太白', '梦鸽'} # v2 = {'李杰', '景女神} # 请得到 v1 和 v2 的交集并输出 # 请得到 v1 和 v2 的 ...
- SpringBoot上传文件,经过spingCloud-Zuul,中文文件名乱码解决办法
最近用springCloud整合springboot做分布式服务发现经过zuul之后上传的中文文件名乱码全都变成?????,从而引发异常,单独用springboot却是好的,在网上找到相关资料总结如下 ...
- JS基础_js编写位置
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 为什么日本编程语言ruby没前途
ruby是日本的编程语言,不像日本生鱼片一样受人喜欢 日本 Ruby的性能不如.NET或Java 你又是对的!另外,Ruby比Erlang,Lua,C ++等慢,但你不使用Erlang或C ++? W ...
- 多线程学习-- part 1 Thread
一.Thread的使用 (1)sleep:进程等一会 (2)join:让并发处理变成串行 (3)start:启动线程的唯一方法,start()首先为线程分配必须的系统资源,调度线程运行并执行线程的ru ...
- MP4 ISO基础媒体文件格式术语
术语.定义和缩略术语 box 由唯一类型标识符和长度定义的面向对象的构造块(注:在一些标准称为“atom") chunk(块) 一个track连续采样集合 container box 唯一目 ...
- QT Qdialog的对话框模式以及其关闭
模式对话框 描述 阻塞同一应用程序中其它可视窗口输入的对话框.模式对话框有自己的事件循环,用户必须完成这个对话框中的交互操作,并且关闭了它之后才能访问应用程序中的其它任何窗口.模式对话框仅阻止访问与对 ...