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\n",ans);
}

int main()
{
  work();
  return 0;
}

转载于:https://www.cnblogs.com/Hxymmm/p/7758020.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值