Travel

Travel

Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 155 Accepted Submission(s): 68
 
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
2008 Asia Chengdu Regional Contest Online
 
Recommend
lcy
/*
题意:给出你n个点,然后m条双向的路。让你输出第i条路毁坏之后,任意两个城市之间最短路的总和,如果i条路毁坏之后
    城市不在联通,那么就输出INF
初步思路:遍历每条边断了,然后dijkstra搞一下最短路,删边操作,只需要记录一下两点之间线路的条数,如果大于两条
    的话,删完这条边的时候,两点间的权值还是为1,否则,删完之后权值为零

#超时:剪一下枝,如果两点间有两条或以上的线路的话,就不用重新dijkstra了,直接调用离线计算好的就行了

#再次超时:最短路写的不行,这里用最短路的效率不如直接用bfs(),因为这里每条路的权值都是1,这样的话bfs的效率会比
    dijkstra高很多。

#还是超时:.....找不出问题烦,加一个剪枝条件,遍历第一遍的时候,将用到的边标记一下,如果删除的时候,这条边根本
    没用到,那么这条边删掉是不影响后边结果的

#继续超时:换成邻接矩阵试试

#感悟:靠,用邻接矩阵就过了
*/
#include<bits/stdc++.h>
#define INF 3000000
using namespace std;
int n,m;
int u[3005],v[3005];
int mapn[110][110];//存储地图
int used[110][110][110];//用来表示用到了哪条边
int dis[110];//表示耗费的路径
int sum[110];//记录每个点的最短路
int vis[110];
vector<int>edge[110];
int res=0;
int dijkstra(int s,bool flag=false){
    memset(vis,0,sizeof vis);
    memset(dis,0,sizeof dis);
    queue<int>q;
    q.push(s);
    vis[s]=1;
    while(!q.empty()){
        int Start=q.front();
        q.pop();
        // if(flag==false)
            // cout<<"s="<<s<<" Start="<<Start<<endl;
        for(int i=0;i<edge[Start].size();i++){
            int Next=edge[Start][i];
            if(mapn[Start][Next]==0) continue;
            // if(flag==false)
            // cout<<"Start="<<Start<<" Next="<<Next<<endl;
            if(!vis[Next]){//这个点没有遍历过
                dis[Next]=dis[Start]+1;
                if(flag==true){
                    used[s][Start][Next]=1;
                    used[s][Next][Start]=1;
                }
                q.push(Next);
                vis[Next]=1;
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++){
        if(i==s) continue;
        if(dis[i]==0){
            return INF;
        }
        res+=dis[i];
    }
    return res;
}
void init(){
    memset(mapn,0,sizeof mapn);
    memset(used,0,sizeof used);
    for(int i=1;i<=100;i++){
        edge[i].clear();
    }
}
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        for(int i=0;i<m;i++){
            scanf("%d%d",&u[i],&v[i]);
            edge[u[i]].push_back(v[i]);
            edge[v[i]].push_back(u[i]);
            mapn[u[i]][v[i]]++;
            mapn[v[i]][u[i]]++;
        }//建图
        // for(int i=1;i<=n;i++){
            // for(int j=1;j<=n;j++){
                // cout<<d_mapn[i][j]<<" ";
            // }
            // cout<<endl;
        // }
        int frist=0;
        for(int i=1;i<=n;i++){
            sum[i]=dijkstra(i,true);
            // cout<<cur<<" ";
            if(sum[i]==INF){
                frist=INF;
                break;
            }
            frist+=sum[i];
        }
        // cout<<endl;
        //cout<<"frist="<<frist<<endl;
        if(frist==INF){
            for(int i=0;i<m;i++)
                printf("INF
");
            continue;
        }
        for(int i=0;i<m;i++){//删除第i条边
            int f=0;
            res=0;
            if(mapn[u[i]][v[i]]>=2){
                printf("%d
",frist);
            }else{
                // cout<<"一条路"<<endl;
                mapn[u[i]][v[i]]=mapn[v[i]][u[i]]=0;//删掉这条边
                // for(int j=1;j<=n;j++){
                    // for(int k=1;k<=n;k++){
                        // cout<<d_mapn[j][k]<<" ";
                    // }
                    // cout<<endl;
                // }
                for(int j=1;j<=n;j++){
                    if(used[j][u[i]][v[i]]==0){//这条边没有用到
                        res+=sum[j];
                        continue;
                    }
                    int cnt=dijkstra(j);
                    // cout<<"i="<<i<<" j="<<j<<" "<<cnt<<endl;
                    if(cnt==INF){
                        f=1;
                        break;
                    }
                    res+=cnt;
                }
                if(f==1) puts("INF");
                else printf("%d
",res);
                mapn[u[i]][v[i]]=mapn[v[i]][u[i]]=1;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6395994.html