传送门:>Here<

题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, dis(s2,r2)<=l2

解题思路

  首先我们会发现,由于边权都为1,删去一些边,某两点间的最短路肯定会随着删的边越来越多而越来越长(捷径被删了)

  因此我们会发现,要让边删的尽量多,最好最后只剩下最短路,其它边都剩下。换句话说,如果两条最短路不相交,那么最后只会剩下孤零零的两条链

  于是我们会发现,我们可以初步得到一个答案:$M - d(s1,t1) - d(s2,t2)$

  但这有可能不是最优的答案——两条最短路有可能有重叠。重叠的部分越长,答案就会越大也就是更优。并且很容易证明,重叠肯定只有连续的一段,而不会有间断的两断——因为如果需要有两断的话肯定不如连起来变为一段来的优。所以可以$O(n^2)$枚举重叠部分,验证一下就好了

Code

  用SPFA预处理出任意两点间的距离,注意数组要开$N*N$

  教训:当出现莫名其妙的错误的时候,首先检查数组有没有开错——例如,int开成bool,开得太小等等

/*By QiXingzhi*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int MAXM = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar(); return x * w;
}
int N,M,x,y,s1,t1,l1,s2,t2,l2,top,sp1;
int first[MAXM*],nxt[MAXM*],to[MAXM*],num_edge;
int d[MAXN][MAXN],inQ[MAXN],pre[MAXN],ans[MAXN],tmp[MAXN];
queue <int> q;
inline void add(int u, int v){
to[++num_edge] = v;
nxt[num_edge] = first[u];
first[u] = num_edge;
}
inline void SPFA(int s, int c){
for(int i = ; i <= N; ++i) d[i][c] = INF;
memset(inQ, , sizeof(inQ));
d[s][c] = ;
q.push(s);
int u,v;
while(!q.empty()){
u = q.front();q.pop();
inQ[u] = ;
for(int i = first[u]; i; i = nxt[i]){
v = to[i];
if(d[u][c] + < d[v][c]){
d[v][c] = d[u][c] + ;
if(!inQ[v]){
inQ[v] = ;
q.push(v);
}
}
}
}
}
int main(){
// freopen(".in","r",stdin);
N=r,M=r;
for(int i = ; i <= M; ++i){
x=r,y=r;
add(x, y), add(y, x);
}
s1=r,t1=r,l1=r;
s2=r,t2=r,l2=r;
for(int i = ; i <= N; ++i){
SPFA(i, i);
}
if(d[s1][t1] > l1 || d[s2][t2] > l2){ printf("-1"); return ; }
int Ans = M - d[s1][t1] - d[s2][t2];
if(Ans < ) Ans = ;
for(int i = ; i <= N; ++i){
for(int j = ; j <= N; ++j){
if(i == j) continue;
if(d[s1][i] + d[j][t1] + d[i][j] <= l1){
if(d[s2][i] + d[j][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[j][t2]));
}
if(d[s2][j] + d[i][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][i]+d[i][j]+d[j][t1]+d[s2][j]+d[i][t2]));
}
}
if(d[s1][j] + d[i][t1] + d[i][j] <= l1){
if(d[s2][i] + d[j][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][j]+d[i][j]+d[i][t1]+d[s2][i]+d[j][t2]));
}
if(d[s2][j] + d[i][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][j]+d[i][j]+d[i][t1]+d[s2][j]+d[i][t2]));
}
}
}
}
printf("%d", Ans);
return ;
}

Codeforces543 B. Destroying Roads的更多相关文章

  1. CF Destroying Roads (最短路)

    Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Codeforces 543.B Destroying Roads

    B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #302 (Div. 1) B - Destroying Roads

    B - Destroying Roads 思路:这么菜的题我居然想了40分钟... n^2枚举两个交汇点,点与点之间肯定都跑最短路,取最小值. #include<bits/stdc++.h> ...

  5. Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

    D - Destroying Roads Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...

  6. [CF544] D. Destroying Roads

    D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. B. Destroying Roads

    Destroying Roads 题目链接 题意 n个点,m条边每两个点之间不会有两个相同的边,然后给你两个起s1,s2和终点t1,t2; 求删除最多的边后满足两个s1到t1距离\(<=l1\) ...

  8. Codeforces 543B Destroying Roads(最短路)

    题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...

  9. [CodeForces] 543B Destroying Roads

    脑洞+暴力. 因为边权是1,所以bfs一下,O(n^2)求任意两点间最短路,再枚举. ans最大是\(dis_{s1,t1}+dis_{s2,t2}\) 再考虑有公共边的情况,一定存在两个点 u, v ...

随机推荐

  1. python爬虫随笔-scrapy框架(1)——scrapy框架的安装和结构介绍

    scrapy框架简介 Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试 ...

  2. java 接口实现防盗门功能

    Door: package locker; public abstract class Door { public abstract void open(); public abstract void ...

  3. python的四种内置数据结构

    对于每种编程语言一般都会规定一些容器来保存某些数据,就像java的集合和数组一样python也同样有这样的结构 而对于python他有四个这样的内置容器来存储数据,他们都是python语言的一部分可以 ...

  4. PS制作恐怖逼真滴血文字

    序言:各位同学们好,今天给大家带来一例恐怖逼真滴血文字效果的制作教程,本人比较喜欢看恐怖游戏,是看不是玩,然后就突发奇想地做了这件作品,最后的效果我很喜欢,而且制作起来难度并不大,在此分享自己在作图时 ...

  5. Python + selenium + pycharm 环境部署细节 和selenium、Jenkins简单介绍

    一.测试体系:Python + selenium + pycharm + Jenkins/docker 环境搭建: 1.安装python 3.4/3.5 2/3.6/ 3.7 2.配置环境变量 3.p ...

  6. vue单页面模板说明文档(3)

    Environment Variables Sometimes it is practical to have different config values according to the env ...

  7. 【学亮开讲】Oracle存储过程教学笔记(一)20181115

    --创建业主序列起始值为11 ; --不带传出参数的存储过程 create or replace procedure pro_owners_add ( v_name varchar2,--名称 v_a ...

  8. vue cli3 vue.config.js 配置详情

    module.exports = {   // 基本路径   baseUrl: process.env.NODE_ENV === 'production'     ? '/'     : '/',   ...

  9. hive自定义函数

  10. Socket构造但不连接

    Socket socket = new Socket(); SocketAddress address = new InetSocketAddress("localhost",80 ...