Travel (BFS + 预处理优化)

Travel

Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 168 Accepted Submission(s): 74
 
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
 
题意: 给你 N 个点 M 条边, 输入M条边,然后 按照输入M条边的顺序,把第i条边删掉,然后求sum,如果不存在sum则输出INF;


首先肯定不能每次都把sum求一遍,复杂度太高 O(n*n*m)。

所以先用BFS预处理一下求最短路,为啥可以用BFS呢,因为权值都为1,所以用BFS就可以了(逃
然后考虑特殊情况  如果有两个结点间有两条边 则可以直接输出;
#include <iostream>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
int N, M;
const int MAXN = 105;

struct Edge {
    int v, u;
}edge[3005];
int prefix[MAXN][MAXN];
bool visited[MAXN];
int map[MAXN][MAXN];
int ans[MAXN];
int BFS(int k, bool is_preprocess = true) {//权制都是1,所以不需要用最短路,直接用BFS就可以。
    int cur;
    int sum = 0;
    int cnt = 1;
    queue<int> que;
    int dis[MAXN] = {0};
    memset(visited, false, sizeof(visited)); //这里visited原来是int,莫名其妙的一直TL。。。让人摸不到头脑.jpg
    cur = k;
    visited[cur] = 1;
    que.push(cur);
    while (!que.empty()) {
        cur = que.front();
        que.pop();
        for (int i = 1; i <= N; ++i) {
            if (!visited[i] && map[cur][i] >= 1) {
                if (is_preprocess)
                    prefix[k][i] = cur;
                visited[i] = true;
                dis[i] = dis[cur] + 1;
                sum += dis[i];
                que.push(i);
                cnt++;
            }
        }
    }
    return (cnt == N) ? sum : -1;
}
int main() {
    
    while (cin >> N >> M) {
        memset(map, 0, sizeof(map));
        memset(prefix, 0, sizeof(prefix));
        memset(ans, 0, sizeof(ans));
        int sum = 0;
        int flag = 0;
        for (int i = 1; i <= M; ++i) {
            cin >> edge[i].v >> edge[i].u;
            map[edge[i].v][edge[i].u]++;
            map[edge[i].u][edge[i].v]++;
        }
        for (int i = 1; i <= N; ++i) {
            ans[i] = BFS(i);
            if (ans[i] == -1) {
                    //cout << i << endl;
                flag = 1;//不能构成连通图的标记
                break;
            }
            sum += ans[i];
        }
        if (flag) {//不能构成连通图,直接输出INF
            for (int i = 1; i <= M; ++i) {
                cout << "INF" << endl;
            }
        } else {
            for (int i = 1; i <= M; ++i) {
                int u = edge[i].u, v = edge[i].v;
                if (map[v][u] > 1) { //边数大于1直接输出
                    cout << sum << endl;
                } else {
                    int temp_sum = 0;
                    flag = 0;
                    for (int j = 1; j <= N; ++j) {//如果割边在第i个最短路径中
                        if (prefix[j][u] == v || prefix[j][v] == u) {
                            map[u][v] = map[v][u] = 0;//删边
                            int temp = BFS(j, false);
                            if (temp == -1) {
                                flag = 1;
                                map[u][v] = map[v][u] = 1;
                                break;
                            }
                            temp_sum += temp;
                            map[u][v] = map[v][u] = 1;
                        } else {
                            temp_sum += ans[j];
                        }
                    }
                    
                    if (flag) {
                        cout << "INF" << endl;
                    } else {
                        cout << temp_sum << endl;
                    }
                }
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值