HDU2433 BFS最短路

來源:互聯網
上載者:User

標籤:addition   判斷   連通   names   limit   pre   stream   rmi   最佳化   

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 Input5 45 11 33 25 42 21 21 2 

 

Sample OutputINFINFINFINF22 

 

Source2008 Asia Chengdu Regional Contest Online題意:有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最短路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.