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. javascript之循环保存数值

    javascript之循环保存数值 语言都是相通的,这句话在我学javascript时有的深刻的意识.js中的for循环与java中的for循环有很大相似之处. 先看下面这段代码 for(var i= ...

  2. Android语录

    1. application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期.因为它是全局的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象.因此在 ...

  3. 【Java EE 学习 57】【酒店会员管理系统之分页模板书写】

    分页一直是一个比较麻烦的问题,特别是在我做的这个系统中更是有大量的分页,为了应对该问题,特地写了一个模板以方便代码重用,该模板包括后台分页的模板.前端显示的模板两部分. 一.分页分析 分页需要三种类型 ...

  4. 【体系结构】动态调度算法:记分牌算法和tomasulo算法

    记分牌和tomasulo算法 动态调度: 通过硬件在程序执行时重新安排代码的执行序列来减少竞争引起的流水线停顿时间 动态调度流水线具备以下功能: (1)允许按序取多条指令和发射多条指令----取指(I ...

  5. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  6. VadRoot枚举进程模块在Windows7下的完整实现

      原理小伟的小伟在http://bbs.pediy.com/showthread.php?t=66886说的挺清楚了,Windows7下有一些变化,使用NtQueryVirtualMemory来枚举 ...

  7. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    java日期格式大全 format SimpleDateFormat(转) SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH ...

  8. redmine整合GIT版本库

    redmine整合GIT版本库   服务器的环境: Ubuntu 11.10 64位 Redmine 1.4.5.stable.10943 git version 1.7.5.4 + gitolite ...

  9. 向上滚动或者向下滚动分页异步加载数据(Ajax + lazyload)[上拉加载组件]

    /**** desc : 分页异步获取列表数据,页面向上滚动时候加载前面页码,向下滚动时加载后面页码 ajaxdata_url ajax异步的URL 如data.php page_val_name a ...

  10. 一个快速排序(分类)及使用类似思想实现选择问题[c++实现]

    一.快速排序(快速分类)算法: 问题描述:给定线性集中n个元素和一个整数k,1<=k<=n,要求找出这n个元素中第k小的元素. 思想:选取数组A中的某个元素 t=A[s],然后将其他元素重 ...