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. 创建进程池与线程池concurrent.futures模块的使用

    一.进程池. 当并发的任务数量远远大于计算机所能承受的范围,即无法一次性开启过多的任务数量就应该考虑去 限制进程数或线程数,从而保证服务器不会因超载而瘫痪.这时候就出现了进程池和线程池. 二.conc ...

  2. Spring Boot(一) Hello World

    一.Spring Boot之我见     Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从 ...

  3. spring boot 整合mybatis 的xml版本【包括逆向工程以及分页插件】

    逆向工程很方便,可以直接根据数据库和配置文件生成pojo,mapper接口和相应的映射文件. xml版本和全注解版本其实差不多,大部分情况下,都会保留xml文件方便其他人去扩展新的dml方法. 文章旨 ...

  4. php数据提交POSt

    通常情况下用户使用浏览器网页表单向服务器post提交数据,我们使用PHP的$_POST接收用户POST到服务器的数据,并进行适当的处理.但有些情况下,如用户使用客户端软件向服务端php程序发送post ...

  5. node项目的基本目录结构

    1.public目录: 项目公共目录,存放静态资源(img.js.css)和公共资源,404错误提示页面: 2.routor目录: 路由控制器目录,存放路由文件,将所有的业务逻辑都都写在这里: 3.v ...

  6. Linux虚拟机--进入MySQL报错的解决办法

    在Linux安装MySQL有时候会出现 [mysql]ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/ ...

  7. 11.Django基础九之中间件

    一 前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上 ...

  8. 带你入门SpringCloud 之 通过SpringCloud Bus 自动更新配置

    前言 在<带你入门SpringCloud统一配置 | SpringCloud Config>中通过 SpringCloud Config 完成了统一配置基础环境搭建,但是并没有实现配置修改 ...

  9. Nodejs 发送邮件 激活邮箱

    1. 安装nodemailer npm install nodemailer 项目中引入nodemailer var nodemailer = require('nodemailer'); 2.QQ邮 ...

  10. 正睿OI国庆day1

    正睿OI国庆day1 T1 \[ S_n=1*S_{n-1}+1*F_{n-1}+1*F_{n-2}+1*f_{n-1}+1*f_{n-2} \] \[ F_{n}=0*S_{n-1}+1*F_{n- ...