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. c模拟c++ const 转换

    #include <stdio.h> int main(){ const int constant = 21; const int* const_p = &constant; in ...

  2. links and softwares

    links 普通 http://www.ncpa-classic.com//special/2014gejujie/index.shtml ; 中国大剧院 http://tieba.baidu.com ...

  3. IO边读边写

      using (FileStream fs = new FileStream(@"C:\Users\Desktop\lijia1.txt",FileMode.Open))     ...

  4. php多态简单示例

    <?php //多态由不同的人执行而产生不同的结果: //一个人通过不同的状态执行同一种动作,产生不同的结果也可称为多态 //多态的原理,就是类都写好了,不要去修改它,只要在类外的的调用参数的更 ...

  5. android studio使用部分报错处理

    1.android studio 导入项目时Error:SSL peer shut down incorrectly 今天导入一个项目到studio,显示在下载一个一个1.1.0-rc4的东西. 过了 ...

  6. Java Static关键字详解

    提起static关键字,相信大家绝对不会陌生,但是,想要完全说明白,猛的一想,发现自己好像又说不太明白... ...比方说,昨天被一个同学问起的时候... ... 当然,不是所有人都像我一样学艺不精的 ...

  7. [BI项目记]-新任务创建

    上一篇介绍了如何处理一个Bug工作,此篇主要介绍如何借助TFS对于一个新需求创建一个新的工作项. 这里假定,有一个新的需求,需要创建五个报表. 然后开发的工作流程如下: 这个流程总结起来大致如下: 首 ...

  8. 大数据项目实践:基于hadoop+spark+mongodb+mysql+c#开发医院临床知识库系统

    一.前言 从20世纪90年代数字化医院概念提出到至今的20多年时间,数字化医院(Digital Hospital)在国内各大医院飞速的普及推广发展,并取得骄人成绩.不但有数字化医院管理信息系统(HIS ...

  9. ethhdr、ether_header、iphdr、tcphdr、udphdr 结构介绍

    转自:http://blog.csdn.net/petershina/article/details/8573853 ************************eth的结构*********** ...

  10. DevExpress.XtraEditors.xtraScrollableControl

    DevExpress.XtraEditors.xtraScrollableControl里面加一个有高度的控件就有滚动条了