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. iOS self

    如果self在对象方法中 那么self就代表调用当前对象方法的那个对象 如果self在类方法中 那么self就代表调用当前类方法的那个类 总结:self的使用只需关注self在哪个方法中 如果在类方法 ...

  2. nginx优化

    此文章非原创,出自鸟哥之手~ http://blog.chinaunix.net/uid-25266990-id-2985541.html 改排版改得多,当然红色部分要注意下,用得较多 ------- ...

  3. BFS

    广(宽)度优先搜索算法(Breadth-First-Search):  BFS是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止.遍历过程如图,一层一层的找(在访问图中某一起始 ...

  4. 在win7环境下批量修改文件权限

    在附件->命令提示符->右键->以管理员身份运行 进入你需要修改的文件位置,然后输入下面两条命令 takeown /f * /A /R icacls * /t /grant:r ev ...

  5. zend studio 13.5破解以及集成xdebug

    环境说明: 操作系统:Windows 7 Ultimate Edition Service Pack 1 PHP:7.0.11 TS Zend Studio:13.5.0 Xdebug:2.5.0 一 ...

  6. HTTP访问错误大全

    400 - 错误的请求. ·401 - 访问被拒绝.IIS 定义了许多不同的 401 错误,它们指明更为具体的错误原因.这些具体的错误代码在浏览器中显示,但不在 IIS 日志中显示: ·401.1 - ...

  7. scp 从远程服务器上一下载文件

    scp -P202 xx3.x6.xx.xx:/usr/local/zookeeper-.zip /tmp #指定远程服务器的端口和远程服务器的目标文件 ,最后指定要下载到本的地目录 也可以从远程服务 ...

  8. 用javascript设置和读取cookie的例子

    请看下面用javascript设置和读取cookie的简单例子,现在的问题是,如果要设置的是一个cookie集,比如在cookie1集中有uname,uid两组信息,应该如何写呢?cookie(&qu ...

  9. Swagger(webapi自动生成接口说明文档)

    1.引入Swagger.Net.UI和Swashbuckle包 2.卸载重复包Swagger.Net 3.多余的SwaggerUI文件夹 4.项目属性->勾选生成xml文档文件 5.添加类Swa ...

  10. CentOS Linux服务器安全设置

    转自:http://www.osyunwei.com/archives/754.html 引言: 我们必须明白:最小的权限+最少的服务=最大的安全 所以,无论是配置任何服务器,我们都必须把不用的服务关 ...