zoukankan      html  css  js  c++  java
  • HDU 2433 Travel

    Travel

    Time Limit: 2000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2433
    64-bit integer IO format: %I64d      Java class name: Main
     
        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

     
    解题:看完别人的代码才知道这题是怎么回事。删除边的时候,每次删一条,但是每个删除是独立的,也就是这次删完后,得马上补回去。
     
    用数组记录源点为x的时候,有那些边被选用了。删除的时候,看各源点求最短路时,是否用了此边,用了的,要再次求最短路。
     
    大量重复边,及其无聊。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 using namespace std;
    16 const int maxn = 110;
    17 int sum[maxn],d[maxn],num[maxn][maxn],n,m;
    18 bool arc[maxn][maxn],used[maxn][maxn][maxn],in[maxn];
    19 int q[maxn*maxn],head,tail,e[3002][2];
    20 int spfa(int s,bool is){
    21     for(int i = 1; i <= n; i++){
    22         in[i] = false;
    23         d[i] = INF;
    24     }
    25     head = tail = d[s] = 0;
    26     in[s] = true;
    27     q[tail++] = s;
    28     while(head < tail){
    29         int u = q[head++];
    30         in[u] = false;
    31         for(int i = 1; i <= n; i++){
    32             if(arc[u][i] && d[i] > d[u]+1){
    33                 d[i] = d[u]+1;
    34                 if(!in[i]){
    35                     in[i] = true;
    36                     q[tail++] = i;
    37                     if(is) used[s][u][i] = used[s][i][u] = true;
    38                 }
    39             }
    40         }
    41     }
    42     int ans = 0;
    43     for(int i = 1; i <= n; i++){
    44         if(d[i] == INF){ans = INF;break;}
    45         ans += d[i];
    46     }
    47     return ans;
    48 }
    49 int main(){
    50     int i,j,u,v,ans;
    51     while(~scanf("%d %d",&n,&m)){
    52         memset(arc,false,sizeof(arc));
    53         memset(used,false,sizeof(used));
    54         memset(num,0,sizeof(num));
    55         memset(sum,0,sizeof(sum));
    56         for(i = 0; i < m; i++){
    57             scanf("%d %d",&u,&v);
    58             arc[u][v] = arc[v][u] = true;
    59             num[u][v]++;
    60             num[v][u]++;
    61             e[i][0] = u;
    62             e[i][1] = v;
    63         }
    64         ans = 0;
    65         for(i = 1; i <= n; i++){
    66             sum[i] = spfa(i,true);
    67             if(sum[i] == INF){ans = INF;break;}
    68             ans += sum[i];
    69         }
    70         int tmp = 0;
    71         for(i = 0; i < m; i++){
    72             u = e[i][0];
    73             v = e[i][1];
    74             int ans2 = ans;
    75             if(ans == INF) puts("INF");
    76             else if(num[u][v] > 1) printf("%d
    ",ans);
    77             else{
    78                 arc[u][v] = arc[v][u] = false;
    79                 for(j = 1; j <= n; j++){
    80                     if(used[j][u][v]){
    81                         tmp = spfa(j,false);
    82                         if(tmp == INF) break;
    83                         ans2 += tmp - sum[j];
    84                     }
    85                 }
    86                 if(tmp == INF) puts("INF");
    87                 else printf("%d
    ",ans2);
    88                 arc[u][v] = arc[v][u] = true;
    89             }
    90         }
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    Linux系统下搭建Gitlab服务器
    Sqlserver数据库表转换成C#实体类
    C# 获取今天,昨天,上周,下周,上月,下月等等一些日期格式【转】
    Funthing
    网页生成海报
    NET 5.0 Swagger API 自动生成MarkDown文档
    说说C# 8.0 新增功能Index和Range的^0是什么?
    tblive-studio vs2019 编译过程记录
    确定已安装的 .NET Framework 版本
    修改CMD默认编码
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3947408.html
Copyright © 2011-2022 走看看