[CF544] D. Destroying Roads
2 seconds
256 megabytes
standard input
standard output
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.
The first line contains two integers n, m (1 ≤ n ≤ 3000, ) — the number of cities and roads in the country, respectively.
Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.
The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n).
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
0
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
1
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
-1
转化题意:我们要留下最少的道路使得s1->t1距离<=l1, s2->t2距离<=l2.
我们考虑最后留下的路的形态, 无非有两种状态:
1.只有从s1->t1, s2->t2的道路。
2.存在两个(或一个)点u, v, 最后留下的边为
(s1, u), (s2, u), (u, v), (v, t1), (v, t2)或者
(s1, u), (t2, v), (u, v), (v, t1), (v, s2)的五元组。
我们根据以上的情况, 先bfs求出每两个点之间的距离, 然后按状态2的两种情况分别找最优解。
然后最后再考虑第一种情况。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define int long long
inline int read(){
int res = ;char ch=getchar();bool fl = ;
while(!isdigit(ch)){if(ch=='-')fl=;ch=getchar();}
while(isdigit(ch)){res=(res<<)+(res<<)+(ch^);ch=getchar();}
return fl?-res:res;
}
const int N = , M = ;
int n, m;
struct edge{
int nxt, to;
}ed[M];
int head[N], cnt;
inline void add(int x, int y){
ed[++cnt] = (edge){head[x], y};
head[x] = cnt;
}
int s1, s2, t1, t2, l1, l2;
int dis[N][N];
bool ex[N];
int ans = 1e9; inline void bfs(int cur)
{
queue <int> q;
memset(dis[cur], 0x3f, sizeof dis[cur]);
memset(ex, , sizeof ex);
dis[cur][cur] = ;
q.push(cur);ex[cur] = ;
while(!q.empty()){
int x = q.front();
q.pop();
ex[x] = ;
for (register int i = head[x] ; i ; i = ed[i].nxt){
int to = ed[i].to;
if (dis[cur][to] > dis[cur][x] + )
{
dis[cur][to] = dis[cur][x] + ;
if (!ex[to]){
ex[to] = , q.push(to);
}
}
}
}
} signed main()
{
n = read(), m = read();
for (register int i = ; i <= m ; i ++){
int x = read(), y = read();
add(x, y), add(y, x);
}
s1 = read(), t1 = read(), l1 = read();
s2 = read(), t2 = read(), l2 = read();
for (register int i = ; i <= n ; i ++) bfs(i);
for (register int i = ; i <= n ; i ++){
for (register int j = ; j <= n ; j ++){
int res1 = dis[s1][i] + dis[i][j] + dis[j][t1];
int res2 = dis[s2][i] + dis[i][j] + dis[j][t2];
if (res1 <= l1 and res2 <= l2) ans = min(ans, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[s2][i] + dis[j][t2]);
res1 = dis[s1][i] + dis[i][j] + dis[j][t1];
res2 = dis[s2][j] + dis[i][j] + dis[i][t2];
if (res1 <= l1 and res2 <= l2) ans = min(ans, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[s2][j] + dis[i][t2]);
}
}
if (dis[s1][t1] <= l1 and dis[s2][t2] <= l2) ans = min(ans, dis[s1][t1] + dis[s2][t2]);
if (ans == 1e9) return puts("-1"), ;
cout << m - ans;
return ;
}
[CF544] D. Destroying Roads的更多相关文章
- CF Destroying Roads (最短路)
Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 543.B Destroying Roads
B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #302 (Div. 1) B - Destroying Roads
B - Destroying Roads 思路:这么菜的题我居然想了40分钟... n^2枚举两个交汇点,点与点之间肯定都跑最短路,取最小值. #include<bits/stdc++.h> ...
- Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路
D - Destroying Roads Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...
- B. Destroying Roads
Destroying Roads 题目链接 题意 n个点,m条边每两个点之间不会有两个相同的边,然后给你两个起s1,s2和终点t1,t2; 求删除最多的边后满足两个s1到t1距离\(<=l1\) ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- Codeforces543 B. Destroying Roads
传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, ...
- [CodeForces] 543B Destroying Roads
脑洞+暴力. 因为边权是1,所以bfs一下,O(n^2)求任意两点间最短路,再枚举. ans最大是\(dis_{s1,t1}+dis_{s2,t2}\) 再考虑有公共边的情况,一定存在两个点 u, v ...
随机推荐
- Winform中使用FastReport实现简单的自定义PDF导出
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- Python连载38-协程、可迭代、迭代器、生产者消费者模型
一.生产者消费者模型 import multiprocessing from time import ctime def consumer(input_q): print("Into con ...
- 带你入门SpringCloud统一配置 | SpringCloud Config
前言 在微服务中众多服务的配置必然会出现相同的配置,如果配置发生变化需要修改,一个个去修改然后重启项目的方案是绝对不可取的.而 SpringCloud Config 就是一个可以帮助你实现统一配置选择 ...
- Swift从入门到精通第十五篇 - 类型转换 初识
类型转换(学习笔记) 环境Xcode 11.0 beta4 swift 5.1 类型转换 类型转换是检查实例类型的一种方法,或者检查来自类层级不同的父类或子类一个实例,用 is 和 as 操作符 为类 ...
- preg_relace_callback不起作用匿名函数不启作用替换字符串中的所有图片
遇到这样的一个需求,即替换新闻正文中的所有图片,将其图片地址补充为完整的地址. 刚开始的时候,采用匿名函数的方法可以使用,但有一个问题,好像是php的匿名函数5.4以前的版本支持的并不好. 然后在内部 ...
- spring boot 整合mybatis 的xml版本【包括逆向工程以及分页插件】
逆向工程很方便,可以直接根据数据库和配置文件生成pojo,mapper接口和相应的映射文件. xml版本和全注解版本其实差不多,大部分情况下,都会保留xml文件方便其他人去扩展新的dml方法. 文章旨 ...
- 视频转成在github的readme中展示项目的gif动图
本文中涉及的FastStone Capture和FFmpeg两个软件的百度网盘链接: 链接:https://pan.baidu.com/s/1D5LO9Qmjl-vwJZfnbAloyQ 提取码:56 ...
- 指针生产网络(Pointer-Generator-Network)原理与实战
0 前言 本文内容主要:介绍Pointer-Generator-Network在文本摘要任务中的背景,模型架构与原理.在中英文数据集上实战效果与评估,最后得出结论.参考的<Get To The ...
- 2018年蓝桥杯java b组第三题
标题:复数幂 设i为虚数单位.对于任意正整数n,(2+3i)^n 的实部和虚部都是整数.求 (2+3i)^123456 等于多少? 即(2+3i)的123456次幂,这个数字很大,要求精确表示. 答案 ...
- 程序员写 2000 行 if else?领导:这个锅我不背
前言 知乎上有小伙伴提了这么一个问题,如何看待陕西省普通话水平测试成绩查询系统?查询系统前端代码就直接给出了身份账号,姓名,证书编号,如果信息是真的,就泄露了这么多考生的信息,白给那种.为什么会发生这 ...