Travel

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2789    Accepted Submission(s): 939

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
题意:
有n个点,m条边,每条边的长度是1,问如果将第i(i<=i<=m)条边删去剩下的边能否将每两个点都联通,如果能输出总的最短路否则输出INF。
代码:
/*
由于每条边的长度都是1,可以用bfs来找出单源最短路最后在把所有的单源最短路加起来。优化,去掉边时先判断这条边
是否是某一点最短路中必须要用到的边,若果是,看看这条边有没有重边,如果也没有重边就只能把那一个点的最短路
重新求一次。
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
int n,m,mp[][],use[][][],vis[],dis[],sum[];//mp[i][j]记录i到j路径条数,use[x][i][j]
//记录x的单源最短路中需不需要用到i和j,sum[i]记录i的单源最短路长度。
int a[],b[];
vector<int>v[];
void init()
{
memset(mp,,sizeof(mp));
memset(sum,,sizeof(sum));
memset(use,,sizeof(use));
for(int i=;i<=;i++){
v[i].clear();
}
}
int bfs(int x,int f)
{
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
queue<int>q;
q.push(x);
vis[x]=;
while(!q.empty()){
int y=q.front();
q.pop();
for(int i=;i<v[y].size();i++){
int z=v[y][i];
if(vis[z]) continue;
if(mp[y][z]<=) continue;//y到z之间是否联通
dis[z]=dis[y]+;
vis[z]=;
q.push(z);
if(!f){ //第一次算最短路时标记x的单源最短路要用到y,z。
use[x][y][z]=;
use[x][z][y]=;
}
}
}
int s=; //求总的最短路
for(int i=;i<=n;i++){
if(i==x) continue;
if(dis[i]==){
return -;
}
s+=dis[i];
}
return s;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
mp[a[i]][b[i]]++;
mp[b[i]][a[i]]++;
v[a[i]].push_back(b[i]);
v[b[i]].push_back(a[i]);
}
int ans=;
for(int i=;i<=n;i++){
sum[i]=bfs(i,);
if(sum[i]==-){
ans=-;
break;
}
ans+=sum[i];
}
for(int i=;i<m;i++){
if(ans==-){ //如果数据本身就不能全部连通
printf("INF\n");
continue;
}
mp[a[i]][b[i]]--; //去掉边ab
mp[b[i]][a[i]]--;
if(mp[a[i]][b[i]]>){ //存在重边,还可以连通
printf("%d\n",ans);
}
else{
int anss=ans;
for(int j=;j<=n;j++){
if(use[j][a[i]][b[i]]==) //用不到就不用重新计算了
continue;
int tem=bfs(j,);
if(tem==-){ //不能连通了
anss=-;
break;
}
anss-=sum[j];
anss+=tem;
}
if(anss==-) printf("INF\n");
else printf("%d\n",anss);
}
mp[a[i]][b[i]]++;
mp[b[i]][a[i]]++;
}
}
return ;
}

HDU2433 BFS最短路的更多相关文章

  1. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  2. 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路

    题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...

  3. 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流

    题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...

  4. BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)

    BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...

  5. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

  6. 【USACO 2.4】Overfencing(bfs最短路)

    H行W列的迷宫,用2*H+1行的字符串表示,每行最多有2*W+1个字符,省略每行后面的空格.迷宫的边界上有且仅有两个出口,求每个点出发到出口的最短路. +-+-+-+-+-+ | | +-+ +-+ ...

  7. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  8. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

  9. POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)

    题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0). 分析:状态压缩,先预处理各点之间的最短路,然后sum[i][buff]表示在i点,状态为buff时所耗时...... ...

随机推荐

  1. gdb 调试多线程

    基本i threads 等操作略过,只谈线程同步.异步控制: 先点到,gdb attach到主线程t1 时,所有线程都会停止,所谓同步异步效果,是指在apply continue到所有线程之后, 再切 ...

  2. c/c++ string

    string类的定义.操作. #include<iostream> #include<string> using namespace std; int main() { // ...

  3. QQ聊天界面模式切换

    1.打开一个聊天窗口 2.按照图上步骤 3.模式 3-1气泡模式 3-2文本模式

  4. 何为SSH协议?

    该文来自百度百科,自我收藏. SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH ...

  5. HTML文档可以包含的内容

    通过不同的标签,HTML文档可以包含不同的内容,比如文本,链接,图片,列表,表格,表单,框架等. 文本 HTML对文本的支持是最丰富的,你可以设置不同级别的标题,分段和换行,可以指定文本的语义和外观, ...

  6. 给numpy矩阵添加一列

    问题的定义: 首先我们有一个数据是一个mn的numpy矩阵现在我们希望能够进行给他加上一列变成一个m(n+1)的矩阵 import numpy as np a = np.array([[1,2,3], ...

  7. 关于P,V操作理解的突破,关于并发设计与并行

    今天又找了一篇博客研究P,V操作.. 发现..它有一个变量没有声明.. 我就换了篇博客..http://c.biancheng.net/cpp/html/2600.html 然后就看懂了.. 关键突破 ...

  8. MIT 6.828 JOS学习笔记16. Lab 2.2

    Part 3 Kernel Address Space JOS把32位线性地址虚拟空间划分成两个部分.其中用户环境(进程运行环境)通常占据低地址的那部分,叫用户地址空间.而操作系统内核总是占据高地址的 ...

  9. 注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API

    1.@RequestParam 我们的超链接:<a href="springMvc/testRequestParam">testRequestParam</a&g ...

  10. 《UML大战需求分析》阅读随笔(四)

    状态机图(State Machine Diagram),状态机图是通过描述某事物状态的改变来展现流程的.一般适用于流程围绕某个事物展开,例如请假的流程就围绕请假条的展开.语法,开始于结束符号,实心圆表 ...