HDU 2433 Travel 最短路树

Travel

Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 149 Accepted Submission(s): 65
 
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
题目大意:

给出 n m  为 n 个点,m条边,然后依次拆去一条边,问 拆去后,这个图还是不是连通的,如果连通输出,每个点到其他点的最短路之和。

思路:

直接走每个最短路,肯定是要超时的,看了别人的思路,建立一个最短路树。
意思是这样的:
我们现在求  x  节点到其他所有节点的做短路,那么我们设置一个数组 
used [ x ] [ u ] [ v ]    表示在以 x 为源点求最短路的时候有没有用到 u v  这条边,因为后边我们要一条一条的去删边,如果没用到这条边,那么我们保留原来的结果就可以了,如果当前拆去的这条边 在求 x 的最短路的时候用到了,那么我们再去更新这个值。
很强势的 题目。
如何求最短路,就是正常的 dis  
然后我想说,这真的不是错觉,cout 比 printf 要慢,。。。虽然不怎么喜欢用 printf  

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXE = 3005, MAXV = 105;
struct node
{
    int a, b;
} cut[MAXE];
int rn, tn, total, totalA;
int town[101][101], sum[MAXV];
bool used[MAXV][MAXV][MAXV];
queue<int> q;
int  vis[MAXV], dis[MAXV];
int bfs(int src, bool mark)
{
    int i;
    while(!q.empty())
        q.pop();
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    q.push(src);
    vis[src] = 1;
    while(!q.empty())
    {
        int num = q.front();
        q.pop();
        for(i=1; i<=tn; i++)
        {
            if(!vis[i] && (town[num][i]>0 || town[i][num]>0))
            {
                vis[i] = 1;
                if(mark == 0)
                {
                    used[src][i][num] = 1;
                    used[src][num][i] = 1;
                }
                dis[i] += dis[num]+1;
                q.push(i);
            }
        }
    }
    int tot = 0;
    for(i=1; i<=tn; i++)
    {
        if(!dis[i] && i != src)
            return -1;
        tot += dis[i];
    }
    return tot;
}
int main()
{
    int i, j;
    int a, b;
    while(~scanf("%d %d", &tn, &rn))
    {
        totalA = 0;
        memset(town, 0, sizeof(town));
        memset(sum, 0, sizeof(sum));
        memset(used, 0, sizeof(used));
        for(i=1; i<=rn; i++)
        {
            scanf("%d %d", &a, &b);
            cut[i].a = a, cut[i].b = b;
            town[a][b] ++;
            town[b][a] ++;
        }
        for(i=1; i<=tn; i++)
        {
            sum[i] = bfs(i, 0);
            if(sum[i] == -1)  /// 如果图是不连通的 
            {
                totalA = -1;
                break;
            }
            else
                totalA += sum[i];
        }

        for(i=1; i<=rn; i++)
        {
            total = totalA ;
            town[cut[i].a][cut[i].b] --;
            town[cut[i].b][cut[i].a] --;
            if( total == -1 )
                printf("INF\n");
            else if(town[cut[i].a][cut[i].b]>0 )
                printf("%d\n", total);
            else
            {
                bool f = 0;
                for(j=1; j<=tn; j++)
                    if(used[j][cut[i].a][cut[i].b] == 1)
                    {
                        total -= sum[j];
                        int tmp = bfs(j, 1); 
                        if(tmp == -1)
                        {
                            f = 1;
                            break;
                        }
                        total += tmp;
                    }
                if(f)
                    printf("INF\n");
                else
                    printf("%d\n", total);
            }
            town[cut[i].a][cut[i].b] ++;
            town[cut[i].b][cut[i].a] ++;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值