Sicily 1904. Travel

来源:互联网 发布:电脑多开软件 编辑:程序博客网 时间:2024/06/03 16:54

1904. Travel

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

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

Sample Output

INFINFINFINF2

2

// Problem#: 1904// Submission#: 3590534// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <string.h>#include <vector>#include <queue>using namespace std;const int INF = 1000000000;const int MAXN = 105;bool edge_exist[MAXN][MAXN];bool edge_used[MAXN][MAXN][MAXN];short edge_num[MAXN][MAXN];vector<int> edge[MAXN];short Medge[3005][2];int sum[MAXN];int N, M;int ans;bool connect;int BFS(int s, bool control) {    int counter = 1, S = 0;    bool vis[MAXN];    memset(vis, false, sizeof(vis));    vis[s] = true;    queue<pair<int, int> > q;    q.push(make_pair(s, 0));    while (!q.empty()) {        int from = q.front().first, step = q.front().second, size = edge[from].size();        q.pop();        for (int i = 0; i < size; i++) {            int to = edge[from][i];            if (edge_exist[from][to] && !vis[to]) {                counter++;                S += step + 1;                q.push(make_pair(to, step + 1));                vis[to] = true;                if (control) edge_used[s][from][to] = edge_used[s][to][from] = true;            }        }    }    if (counter != N) return INF;    else return S;}void input() {    for (int i = 0; i < M; i++) {        scanf("%d%d", &Medge[i][0], &Medge[i][1]);        if (!edge_exist[Medge[i][0]][Medge[i][1]]) {            edge[Medge[i][0]].push_back(Medge[i][1]);            edge[Medge[i][1]].push_back(Medge[i][0]);        }        edge_exist[Medge[i][0]][Medge[i][1]] = true;        edge_exist[Medge[i][1]][Medge[i][0]] = true;        edge_num[Medge[i][0]][Medge[i][1]]++;        edge_num[Medge[i][1]][Medge[i][0]]++;    }}void process() {    for (int i = 1; i <= N; i++) sum[i] = INF;    for (int i = 1; i <= N; i++) {        sum[i] = BFS(i, true);        if (sum[i] == INF) {            connect = false;            break;        } else ans += sum[i];    }    if (!connect) {        while (M--) printf("INF\n");    } else {        for (int i = 0; i < M; i++) {            if (edge_num[Medge[i][0]][Medge[i][1]] > 1) printf("%d\n", ans);            else {                int from = Medge[i][0], to = Medge[i][1], new_ans = 0;                edge_exist[from][to] = false;                edge_exist[to][from] = false;                for (int j = 1; j <= N; j++) {                    if (edge_used[j][from][to]) {                        int temp = BFS(j, false);                        if (temp == INF) {                            new_ans = INF;                            printf("INF\n");                            break;                        } else {                            new_ans += temp;                        }                    } else {                        new_ans += sum[j];                    }                }                if (new_ans != INF) printf("%d\n", new_ans);                edge_exist[from][to] = true;                edge_exist[to][from] = true;            }        }    }}void init() {    ans = 0;    connect = true;    memset(edge_exist, false, sizeof(edge_exist));    memset(edge_num, 0, sizeof(edge_num));    memset(edge_used, false, sizeof(edge_used));    for (int i = 1; i <= N; i++) edge[i].clear();}int main() {    while (scanf("%d%d", &N, &M) != EOF) {        init();        input();        process();    }    return 0;}                                 


0 0
原创粉丝点击