• Codeforces Round #302 B. Destroying Roads


    Description

    In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.

    You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.

    Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.

    Translation

    在保证 s1-t1 的最短路<=l1,s2-t2 的最短路<=l2的情况下,问最多能删除多少条边.

    solution

    因为边权为1,所以非常好做,我们显然是要保留最短路上的边最优,我们考虑两种情况:
    1.两者的最短路不相交,答案即为m-两者最短路之和
    2.相交,我们枚举两个点之间的路径作为公共部分,然后再减去公共部分

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #define il inline
    #define RG register
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    const int N=3005,M=6005;
    int head[N],to[M<<1],nxt[M<<1],num=1,n,m;bool d[M<<1];
    il void link(int x,int y){nxt[++num]=head[x];to[num]=y;head[x]=num;}
    int dis[N][N],q[3000005];
    int bfs(int S){
      int x,u;RG int t=0,sum=1;
      q[1]=S;dis[S][S]=0;
      while(t!=sum){
        x=q[++t];
        for(int i=head[x];i;i=nxt[i]){
          u=to[i];if(d[i])continue;
          if(dis[S][u] || u==S)continue;
          dis[S][u]=dis[S][x]+1;
          q[++sum]=u;
        }
      }
      return M;
    }
    void work()
    {
      int x,y;
      scanf("%d%d",&n,&m);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        link(x,y);link(y,x);
      }
      int s1,s2,t1,t2,l1,l2;
      scanf("%d%d%d",&s1,&t1,&l1);
      scanf("%d%d%d",&s2,&t2,&l2);
      for(int i=1;i<=n;i++)
         bfs(i);
      if(dis[s1][t1]>l1 || dis[s2][t2]>l2){
         puts("-1");
         return ;
      }
      int ans=m-dis[s1][t1]-dis[s2][t2];
      for(int i=1;i<=n;i++){
         for(int j=1;j<=n;j++){
            if(i==j)continue;
            if(dis[s1][i]+dis[j][t1]+dis[i][j]<=l1){
               if(dis[s2][i]+dis[i][j]+dis[j][t2]<=l2)
                  ans=Max(ans,m-dis[s1][i]-dis[j][t1]-dis[i][j]-dis[s2][i]-dis[j][t2]);
               if(dis[s2][j]+dis[i][j]+dis[i][t2]<=l2)
                  ans=Max(ans,m-dis[s1][i]-dis[j][t1]-dis[i][j]-dis[s2][j]-dis[i][t2]);
            }
         }
      }
      printf("%d
    ",ans);
    }
    
    int main()
    {
      work();
      return 0;
    }
    
    
  • 相关阅读:
    linux内存管理之数据结构必备
    Script快速入门与查表
    Bash编程linux诸多熟记命令
    NandFlash/NorFlash源码模型和驱动编写方法
    linux内存管理之uboot第一步
    《Magus Night》
    《P2447 [SDOI2010]外星千足虫》
    DFS 树的理解
    《2021CCPC桂林》
    《GRAPH》
  • 原文地址:https://www.cnblogs.com/Hxymmm/p/7758020.html
Copyright © 2020-2023  润新知