D. Destroying Roads
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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).

Output

Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

Examples
Input

Copy
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
Output

Copy
0
Input

Copy
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
Output

Copy
1
Input

Copy
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
Output

Copy
-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的更多相关文章

  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. B. Destroying Roads

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

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

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

  8. Codeforces543 B. Destroying Roads

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

  9. [CodeForces] 543B Destroying Roads

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

随机推荐

  1. Java第三次作业第二题

    2. [请复制本程序,作为java程序代码,进行编译,补充填写缺失代码部分,并实现题目要求功能,从而获得空白填写所需的内容.] 定义3个线程,模拟红绿灯的效果 一个线程控制画一个实心红圆 一个线程控制 ...

  2. prometheus-operator告警模块alertmanager注意事项(QQ邮箱发送告警)--大大坑

    在/stable/prometheus-operator/values.yaml配置告警邮件 config: global: resolve_timeout: 5m smtp_smarthost: ' ...

  3. ASP.NET Core 2.2 : 二十三. 深入聊一聊配置的内部处理机制

    上一章介绍了配置的多种数据源被注册.加载和获取的过程,本节看一下这个过程系统是如何实现的.(ASP.NET Core 系列目录) 一.数据源的注册 在上一节介绍的数据源设置中,appsettings. ...

  4. 实现一个正则表达式引擎in Python(一)

    前言 项目地址:Regex in Python 开学摸鱼了几个礼拜,最近几天用Python造了一个正则表达式引擎的轮子,在这里记录分享一下. 实现目标 实现了所有基本语法 st = 'AS342abc ...

  5. [Linux] linux下vim对于意外退出的文档的再次开启

    转载自博客:https://blog.csdn.net/ljp1919/article/details/48372615 1.对于同一个文件如果上次已经打开,而未关闭的情况下,又打开该文件进行编辑时, ...

  6. python实现感知机线性分类模型

    前言 感知器是分类的线性分类模型,其中输入为实例的特征向量,输出为实例的类别,取+1或-1的值作为正类或负类.感知器对应于输入空间中对输入特征进行分类的超平面,属于判别模型. 通过梯度下降使误分类的损 ...

  7. uni-app实现滑动切换效果

    在对于uni-app框架了解之后,今天就实现一个滚动切换tab效果,这个很常见的一个效果,最后封装成一个组件,便于以后使用,写这个需要引入uni官方提供的uni.css样式,用到了写好的样式,就不需要 ...

  8. Spring 梳理-AOP

    界面应用场景 日志.声明式事务.安全.缓存 AOP功能演化图 图片引用地址:https://www.cnblogs.com/best/p/5679656.html AOP设计模式-代理模式 静态代理: ...

  9. jquery 取得select选中的值

    1.取得选中的值 jQuery("#select").val();是取得选中的值 2.取得的文本 jQuery("#select  option:selected&quo ...

  10. 【SQL server基础】获取当前时间并固定格式

    Select CONVERT(varchar(), GETDATE(), ): // Select CONVERT(varchar(), GETDATE(), ): Select CONVERT(va ...