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. ActiveMQ的安装与使用。

    1.什么是ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE .4规范的 JMS Provider实现,尽 ...

  2. celery详解

    目录 Celery详解 1.背景 2.形象比喻 3.celery具体介绍 3.1 Broker 3.2 Backend 4.使用 4.1 celery架构 4.2 安装redis+celery 4.3 ...

  3. .NET分布式大规模计算利器-Orleans(一)

      写在前面 Orleans是基于Actor模型思想的.NET领域的框架,它提供了一种直接而简单的方法来构建分布式大规模计算应用程序,而无需学习和应用复杂的并发或其他扩展模式.我在2015年下半年开始 ...

  4. JAVA数据处理的常用技术

    背景 在实际开发中,数据的处理有五种:获取.传输.存储.分析.转换.每种各对应一些常用的技术. 序列化和反序列化 序列化是将对象的信息转换为可传输或可存储形式的过程.反序列化就是反过来让这些可传输的. ...

  5. html常见的块元素与内联(行内)元素用法说明(一)

    html平时常见的块元素有:div, p, h1, h2, h3等,内联元素有:span, a, img等. 块元素的属性:无论内容是什么,都会独占一整行.主要用于页面布局. 内联元素的属性:只占自身 ...

  6. 略学扩展Eculid算法

    扩展 Euclid 算法 Euclid 算法 辗转相除法 计算两个数最大公因数 \(\text{gcd}(a,\,b) = \text{gcd}(b,\,a\%b)\) exEuclid 算法 裴蜀定 ...

  7. 商用hadoop集群的配置命令分布

    角色 安装 hdfs配置 yarn配置 hdfs 格式化 启动yarn服务 启动hdfs服务 master yum install hadoop-hdfs-namenode yum install h ...

  8. Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

  9. Mycat 配置文件schema.xml

    1.介绍 schema.xml 作为 MyCat 中重要的配置文件之一,管理着 MyCat 的逻辑库.表.分片规则. DataNode 以及 DataSource. 2.schema相关标签 sche ...

  10. ps 将图片四角变成圆角

    1.用PS打开一张图片,用矩形选框工具,选出你要保留的的那一部分,“选择→修改→平滑”.在弹出的选框里添入数值,值越大角就越圆. 2.选择“选择→反选”,再按delete删除就ok了.