您的位置:首页 > 其它

hdu2433

2015-09-11 20:26 316 查看

Travel

Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2163 Accepted Submission(s): 721



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

//边太多,所以不采用邻接表建
//continue尽量少用 
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 99999999
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=2*3000+100;
int sum[maxn];
int n;
struct edge{
    int from,to;
}e[maxn];
int first;
int mp[101][101];
int d[101][101][101];
int dis[101];
int vis[101];
int nd[maxn];

int bfs(int u){
    mem0(vis);
    mem0(dis);
    queue<int>Q;
    Q.push(u);
    vis[u]=1;
    int count_=1;
    int sum1=0;
    while(!Q.empty()){
        int v=Q.front();
        Q.pop();
        for(int i=1;i<=n;i++){
            if(vis[i]==0&&mp[v][i]){
                if(first==1)
                    d[u][v][i]=d[u][i][v]=1;
                vis[i]=1;
                dis[i]=dis[v]+1;
                sum1+=dis[i];
                count_++;
                Q.push(i);
            }
        }
    }
    if(count_==n)
        return sum1;
    return INF;
}

int main(){
    int m;
    int u,v;
    //freopen("2433.in","r",stdin);
    //freopen("2433.out","w",stdout);
    while(scanf("%d%d",&n,&m)!=EOF){
        mem0(d);
        mem0(sum);
        mem0(mp);
        for(int k=1;k<=m;k++){
            scanf("%d%d",&u,&v);
            mp[u][v]++;
            mp[v][u]++;
            e[k].from=u;
            e[k].to=v;
        }
        int flag1=1;
        int sum2=0;
        first=1;
        for(int j=1;j<=n;j++){
            sum[j]=bfs(j);
            sum2+=sum[j];
            if(sum[j]==INF){
                flag1=0;
                break;
            }
        }
        if(flag1==0){
            for(int i=1;i<=m;i++)
                printf("INF\n");
            continue;
        }
        first=0;
        for(int k=1;k<=m;k++){
            int sum1=0;
            if(mp[e[k].from][e[k].to]>1){
                printf("%d\n",sum2);
            }
            else if(mp[e[k].from][e[k].to]==1){
                int j;
                for(j=1;j<=n;j++){
                    if(d[j][e[k].from][e[k].to]==0){
                        sum1+=sum[j];
                    }
                    else{
                        mp[e[k].from][e[k].to]--;
                        mp[e[k].to][e[k].from]--;
                        sum1+=bfs(j);
                        mp[e[k].from][e[k].to]++;
                        mp[e[k].to][e[k].from]++;
                        if(sum1>=INF){
                            break;
                        }
                    }
                }
                if(j<=n)
                    printf("INF\n");
                else
                    printf("%d\n",sum1);
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: