B. 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 nm (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 aibi (1 ≤ ai, bi ≤ nai ≠ 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
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
output
0
input
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
output
1
input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
output
-1

题目大意:给定一张边权均为1的无向图。 问至少需要保留多少边,使得s1到t1的最短路不超过l1,s2到t2的最短路不超过l2。

分析:其实就是求最后s1到t1最短路和s2到t2最短路的路径并嘛.

   画几个图会发现最后路径的形式一定是分叉的四段加上重叠的一段(每一段都可能为空).只需要保留这些边,也就是保证了只有一条路可达.那么枚举这个重叠部分的两端,计算一下两端分别到s1,t1,s2,t2的最短路,最后更新答案即可.这一步可以预处理得到.

   坑点:重叠部分可能为一个点;做一次后s1,t1要交换!

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 0x7ffffff;
int n,m,head[maxn],to[maxn * maxn],nextt[maxn * maxn],tot = ,d[maxn][maxn],vis[maxn];
int s1,t1,l1,s2,t2,l2,ans; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void bfs(int x)
{
for (int i = ; i <= n; i++)
d[x][i] = inf;
memset(vis,,sizeof(vis));
queue <int> q;
q.push(x);
vis[x] = ;
d[x][x] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (d[x][v] > d[x][u] + )
{
d[x][v] = d[x][u] + ;
if (!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for (int i = ; i <= m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for (int i = ; i <= n; i++)
bfs(i);
scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2);
if (d[s1][t1] > l1 || d[s2][t2] > l2)
puts("-1");
else
{
ans = d[s1][t1] + d[s2][t2];
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (d[i][j] >= inf)
continue;
int temp1 = d[s1][i] + d[i][j] + d[j][t1];
int temp2 = d[s2][i] + d[i][j] + d[j][t2];
if (temp1 > l1 || temp2 > l2)
continue;
int temp = temp1 + temp2 - d[i][j];
ans = min(temp,ans);
}
}
swap(s1,t1);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (d[i][j] >= inf)
continue;
int temp1 = d[s1][i] + d[i][j] + d[j][t1];
int temp2 = d[s2][i] + d[i][j] + d[j][t2];
if (temp1 > l1 || temp2 > l2)
continue;
int temp = temp1 + temp2 - d[i][j];
ans = min(temp,ans);
}
}
printf("%d\n",m - ans);
} return ;
}

Codeforces 543.B Destroying Roads的更多相关文章

  1. codeforces 544 D Destroying Roads 【最短路】

    题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...

  2. Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

    D - Destroying Roads Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...

  3. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. Codeforces Round #302 (Div. 1) B - Destroying Roads

    B - Destroying Roads 思路:这么菜的题我居然想了40分钟... n^2枚举两个交汇点,点与点之间肯定都跑最短路,取最小值. #include<bits/stdc++.h> ...

  5. Codeforces 543 B. World Tour

    http://codeforces.com/problemset/problem/543/B 题意: 给定一张边权均为1的无向图. 问至多可以删除多少边,使得s1到t1的最短路不超过l1,s2到t2的 ...

  6. CF Destroying Roads (最短路)

    Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  8. Codeforces 806 D.Prishable Roads

    Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...

  9. [CF544] D. Destroying Roads

    D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. mysql数据导到本地

    需求: 把mysql查询结果导出到txt(其他格式亦可),放在本地,供下一步使用 首先网上查了下,select * from driver into outfile 'a.txt'; 前面是你的sql ...

  2. mac安装pkg 一直“正在验证” 卡着

    今天换了新mac, 但是之前wireshark(抓包工具) 不能用了 ,要安装Xquartz. 下载之后一直卡着, 网上找了半天没有解决方法. 最后我重启一下就好了... 重启一下. 2. 15款ma ...

  3. PLSQL变量和类型,流程控制语句,集合

    ---PLSQL 调试授权 GRANT debug any procedure, debug connect session TO scott; --定义变量 declare part_number ...

  4. 该用哪个:Redis与Memcached之间如何选择呢?

    华为云分布式缓存Redis5.0和Memcached都是华为云DCS的核心产品. 那么在不同的使用场景之下,如何选择Redis5.0和Memcached呢? 就由小编为大家进行详细的数据对比分析吧 R ...

  5. Kafka安装之三 spring-kafka实践

    一.spring-kafka配置详解 1.1 要是用spring-kafka 我们首先要在pom要.xml中引入spring-kafka包 <dependencies> <depen ...

  6. 使用qemu启动dd制作的img镜像

    1. 准备工作 应用场景 在需要单机取证时,需要在不影响对象业务的情况下进行取证或分析,可以使用dd 对目标服务器进行镜像,生成img文件,镜像可以通过winhex进行静态分析.但是想要动态分析服务器 ...

  7. jquery选择器 直观实验

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 结对作业_Two

    Part 1.前言 (附:本次编码涵盖的所有功能均为java语言实现) 结对项目作业 结对同学高裕翔的博客 个人github传送门 博文pdf链接 Part 2.具体分工 本次的结对作业我们简单的拆分 ...

  9. Lucene 分词

    在Lucene中很多数据是通过Attribute进行存储的 步骤是同过TokenStrem获取文本信息流 TokenStream stream = a.tokenStream("conten ...

  10. JQuery EasyUI 引用加载分析

    easyui是什么,就不介绍了,接触到前端的就算没用过,肯定也应该听说过.其次,本文不是介绍它提供如calendar.tree等这些功能如何使用的,这些官网上介绍都很详细,中文的网上也不少.本文是从e ...