zoukankan      html  css  js  c++  java
  • hdu 2433 Travel(还不会)

    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

    #include<queue>
    #include<stack>
    #include<vector>
    #include<math.h>
    #include<stdio.h>
    #include<numeric>//STL数值算法头文件
    #include<stdlib.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<functional>//模板类头文件
    using namespace std;
    #define N 110
    #define M 6020
    const int INF=0x3f3f3f3f;
    const int maxn=1010;
    
    struct Edgs
    {
        int to,next;
    } E[M];
    
    struct Node
    {
        int x, y;
    } node[M];
    
    int d[N], pre[N][N], num[N][N], sum[N], head[N];
    int cnt, n, m;
    bool flag = true;
    bool vis[N];
    
    void add_edgs(int u, int v)
    {
        E[cnt].to = v;
        E[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    void init()
    {
        memset(num, 0, sizeof(num));
        memset(head, -1, sizeof(head));
        cnt = 0;
        int x, y;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &x, &y);
            node[i].x = x;
            node[i].y = y;
            num[x][y]++;
            num[y][x]++;
            add_edgs(x, y);
            add_edgs(y, x);
        }
    }
    
    void bfs(int s)
    {
        queue<int> q;
        for (int i = 1; i <= n; i++)
        {
            d[i] = INF;
            vis[i] = false;
        }
        d[s] = 0;
        pre[s][s] = 0;
        vis[s] = 1;
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
    
            for (int i = head[u]; i != -1; i = E[i].next)
            {
                int v = E[i].to;
                if (!vis[v])
                {
                    d[v] = d[u] + 1;
                    vis[v] = 1;
                    pre[s][v] = u;
                    q.push(v);
                }
            }
        }
        sum[s] = 0;
        for (int i = 1; i <= n; i++)
        {
            if (d[i] == INF)
            {
                flag = false;
                return ;
            }
            sum[s] += d[i];
        }
    }
    
    int bfs2(int s)
    {
        queue<int> q;
        for (int i = 1; i <= n; i++)
        {
            vis[i] = false;
            d[i] = INF;
        }
        d[s] = 0;
        vis[s] = true;
        q.push(s);
    
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
    
            for (int i = head[u]; i != -1; i = E[i].next)
            {
                int v = E[i].to;
                if (num[u][v] && !vis[v])
                {
                    d[v] = d[u] + 1;
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            if (d[i] == INF)
                return -1;
            ans += d[i];
        }
        return ans;
    }
    
    void solve()
    {
    
        flag = true;
        for (int i = 1; i <= n; i++)
        {
            if (flag)
                bfs(i);
            else
                break;
        }
    
        for (int i = 0; i < m; i++)
        {
    
            if (!flag)
            {
                printf("INF
    ");
                continue;
            }
            int x = node[i].x;
            int y = node[i].y;
    
            int ans = 0, j;
            for (j = 1; j <= n; j++)
            {
                if (pre[j][x] != y && pre[j][y] != x)
                {
                    ans += sum[j];
                    continue;
                }
                num[x][y]--;
                num[y][x]--;
    
                int t = bfs2(j);
    
                num[y][x]++;
                num[x][y]++;
    
                if (t == -1)
                {
                    printf("INF
    ");
                    break;
                }
                ans += t;
            }
            if (j == n + 1)
                printf("%d
    ", ans);
        }
    }
    
    int main()
    {
        while (scanf("%d%d", &n, &m) != EOF)
        {
            init();
            solve();
        }
        return 0;
    }
    
    























  • 相关阅读:
    人工智能与信息社会——人工智能发展简史
    人工智能与信息社会——新闻热点与身边的人工智能
    蓝桥杯——入门训练
    Android Studio安装虚拟机步骤
    Android开发环境搭建教程
    Android系统架构(图解)
    第6.7节 stack 1982 问题 B: Problem E
    Matlab高级教程_第二篇:MATLAB和C#对应数据类型的讲解(多讲一点儿C#的矩阵运算)
    Matlab高级教程_第二篇:一个简单的混编例子
    Matlab高级教程_第二篇:Matlab2016a和VS2013混合
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264865.html
Copyright © 2011-2022 走看看