[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 ...
随机推荐
- 开源导入导出通用库Magicodes.ExporterAndImporter发布
导入导出通用库 Magicodes.ExporterAndImporter为心莱团队封装的导入导出通用库,并且仍在跟随项目不断地打磨. GitHub地址: https://github.com/xin ...
- STL容器(Stack, Queue, List, Vector, Deque, Priority_Queue, Map, Pair, Set, Multiset, Multimap)
一.Stack(栈) 这个没啥好说的,就是后进先出的一个容器. 基本操作有: stack<int>q; q.push(); //入栈 q.pop(); //出栈 q.top(); //返回 ...
- IO流的工具类
1.需要先导入jar包: FilenameUtils import org.apache.commons.io.FilenameUtils; public class FilenameUtilesDe ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(二十二):如何安装 Nuget(dll) 后使用项目源代码调试
最近碰到开发者问:我使用 nuget 安装了 Senparc.Weixin SDK,但是有一些已经封装好的过程想要调试,我又不想直接附加源代码项目,这样就没有办法同步更新了,我应该怎么办? 这其实是一 ...
- preg_relace_callback不起作用匿名函数不启作用替换字符串中的所有图片
遇到这样的一个需求,即替换新闻正文中的所有图片,将其图片地址补充为完整的地址. 刚开始的时候,采用匿名函数的方法可以使用,但有一个问题,好像是php的匿名函数5.4以前的版本支持的并不好. 然后在内部 ...
- 睡梦中被拉起来执行Spring事务
梦中惊醒 在Tomcat的线程池里,有这样一个线程,自打出生后,从来不去干活儿,有好多次走出线程池“这座大山”去看世界的机会,都被他拱手让给了弟兄们. 弟兄们给他取了个名字叫二师兄.没错,好吃懒做,饱 ...
- [C++] 头文件中不要用using namespace std
先总结下: 1. using namespce std:尽量不要(或者强硬一点,不许)在头文件中使用. 解析: 不让这么用,主要原因就是防止名字重复(即自定义变量名和std中名字重复),因为头文件会被 ...
- 公开的免费WebService接口分享,用于做接口练习
本文转载于 https://cloud.tencent.com/developer/article/1349603 天气预报Web服务,数据来源于中国气象局 Endpoint http://www.w ...
- django开发后台接口error 10053/10054
初学Django,开发完接口之后访问post请求的接口遇到error10053和10054,查阅很多资料没有找到具体的原因. 在这里记录下我遇到这两个报错的原因和解决方案: get请求取请求参数:su ...
- Tomcat+Nginx+Linux+Mysql部署豆瓣TOP250的项目到腾讯云服务器
写在前面 因为前面有写过一篇关于豆瓣的top250的电影的可视化展示项目,你可以移步http://blog.csdn.net/liuge36/article/details/78607955了解这个项 ...