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. ViewPort <meta>标记

    ViewPort <meta>标记用于指定用户是否可以缩放Web页面,如果可以,那么缩放到的最大和最小缩放比例是什么.使用ViewPort <meta>标记还表示文档针对移动设 ...

  2. php-fpm配置

    [global] error_log = /letv/log/php-fpm_error.log [www] user = apache group = apache listen = 127.0.0 ...

  3. ubuntu16.04卸载火狐,Amazon

    一.卸载火狐: . dpkg --get-selections |grep firefox .sudo apt-get purge firefox unity-scope-firefoxbookmar ...

  4. Alpha事后诸葛(团队)

    [设想和目标] Q1:我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? "小葵日记"是为了解决18-30岁年轻用户在记录生活时希望得到一美体验友好 ...

  5. DS06--图

    一.学习总结 1.图的思维导图 2.图学习体会 深度优先遍历与广度优先遍历 不同点:广度优先搜索,适用于所有情况下的搜索,但是深度优先搜索不一定能适用于所有情况下的搜索.因为由于一个有解的问题树可能含 ...

  6. 用C++实现简单随机二元四则运算

    让我们想看看二元四则运算都需要实现什么: (1) 定制题目数量 (2) 是否有乘除法 (3) 题目数值范围 (4) 加减有无负数 (5) 除法有无余数 (6) 是否支持分数(真分数.假分数…) (7) ...

  7. Swift-闭包使用及解决循环引用问题

    Swift中闭包使用参考OC中block使用,基本一致 // 闭包类型 首先写(参数列表)->(返回值类型) func loadData(callBack : (jsonData:String) ...

  8. YARN中用的作业调度算法:DRF(Dominant Resource Fairness)

    在Mesos和YARN中,都用到了dominant resource fairness算法(DRF),它不同于hadoop基于slot-based实现的fair scheduler和capacity ...

  9. Qt使用QNetworkAccessManager实现Http操作

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt使用QNetworkAccessManager实现Http操作     本文地址:http ...

  10. 1st 构建之法读后感

    构建之法读后感 由于时间和书的篇幅所限,所以我没能真正通读全书,只通过网上的介绍和书内前言及目录,大概了解了构建之法是一本怎样的一本书. 这本书是由具有长达20年一线软件开发经验的邹欣老师所撰写,他以 ...