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. js 数组删去重复的加上没有的元素

    为了一个数组的删除操作竟然费了一个多小时,下面分享一下我的代码: 代码功能:判断数组里是否有我要看的元素,如果没有就添加到数组里,如果有就去掉. var selectArr=[]; function ...

  2. qt 导入现有的工程不能运行的问题

    新导入的工程需在qtcreator的项目选项的构建位置配置一下

  3. linux history命令显示时间

    在CentOS上使用history查看历史使用的CMD记录时,发现没有时间,在当前用户的.bash_profile里面,添加 export HISTTIMEFORMAT="%F %T  `w ...

  4. JS中,!=, !== 和 !的区别和使用场景

    var num = 1; var str = '1'; var test = 1;   test == num   //true 相同类型 相同值 test === num  //true 相同类型 ...

  5. 将一个实体数据保存到不同的数据表中<EntityFramework6.0>

    2014-11-22声明方式 public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public ...

  6. MSSQL2005后版本插入数据返回ID的新写法

    例子: INSERT VolunteerSound_Table (Title,ArticleContent)OUTPUT Inserted.ID VALUES ('FirstVal','bbbbb') ...

  7. 安装ganglia

    安装ganglia 1.默认已经配置好相关的主机名和Ip地址映射关系 2.默认已经安装好ssh密码登陆 3.默认已经配置好yum源和相关网络配置(如hosts 可在墙外) 4.服务器端安装(除了yum ...

  8. 大话css之display的Block未解之谜(一)

    用了几年的css了,css中inline | block |inline-block|table|flex从来没有做过系统的整理和分析,网上的分析文章也很多,零散. 今天有空,就在这做一下整理分析 b ...

  9. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  10. [NHibernate]第一个NHibernate的应用配置

    NHibernate是.Net平台下一个成熟的,开源的对象关系映射器(ORM).本文来介绍第一次使用NHibernate的时候的配置. 1.下载NHibernate.Nhibernate官网最新版本为 ...