HDU2433 BFS最短路

Posted LuZhiyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2433 BFS最短路相关的知识,希望对你有一定的参考价值。

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[102][102],use[102][102][102],vis[102],dis[102],sum[102];//mp[i][j]记录i到j路径条数,use[x][i][j]
//记录x的单源最短路中需不需要用到i和j,sum[i]记录i的单源最短路长度。
int a[3003],b[3003];
vector<int>v[102];
void init()
{
    memset(mp,0,sizeof(mp));
    memset(sum,0,sizeof(sum));
    memset(use,0,sizeof(use));
    for(int i=1;i<=100;i++){
        v[i].clear();
    }
}
int bfs(int x,int f)
{
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    queue<int>q;
    q.push(x);
    vis[x]=1;
    while(!q.empty()){
        int y=q.front();
        q.pop();
        for(int i=0;i<v[y].size();i++){
            int z=v[y][i];
            if(vis[z]) continue;
            if(mp[y][z]<=0) continue;//y到z之间是否联通
            dis[z]=dis[y]+1;
            vis[z]=1;
            q.push(z);
            if(!f){           //第一次算最短路时标记x的单源最短路要用到y,z。
                use[x][y][z]=1;
                use[x][z][y]=1;
            }
        }
    }
    int s=0;        //求总的最短路
    for(int i=1;i<=n;i++){
        if(i==x) continue;
        if(dis[i]==0){
            return -1;
        }
        s+=dis[i];
    }
    return s;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        for(int i=0;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=0;
        for(int i=1;i<=n;i++){
            sum[i]=bfs(i,0);
            if(sum[i]==-1){
                ans=-1;
                break;
            }
            ans+=sum[i];
        }
        for(int i=0;i<m;i++){
            if(ans==-1){       //如果数据本身就不能全部连通
                printf("INF\n");
                continue;
            }
            mp[a[i]][b[i]]--;    //去掉边ab
            mp[b[i]][a[i]]--;  
            if(mp[a[i]][b[i]]>0){  //存在重边,还可以连通
                printf("%d\n",ans);
            }
            else{
                int anss=ans;
                for(int j=1;j<=n;j++){
                    if(use[j][a[i]][b[i]]==0)  //用不到就不用重新计算了
                        continue;
                    int tem=bfs(j,1);
                    if(tem==-1){   //不能连通了
                        anss=-1;
                        break;
                    }
                    anss-=sum[j];
                    anss+=tem;
                }
                if(anss==-1) printf("INF\n");  
                else printf("%d\n",anss);
            }
            mp[a[i]][b[i]]++;
            mp[b[i]][a[i]]++;
        }
    }
    return 0;
}

 

以上是关于HDU2433 BFS最短路的主要内容,如果未能解决你的问题,请参考以下文章

hdu 2433 Travel (最短路树)

SPFA_queue_链式前向星最短路 & HDU2433

hdu1372 BFS求最短路径长度

HDU 5876 Sparse Graph BFS 最短路

hdu 5876 Sparse Graph 无权图bfs求最短路

hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)