题目链接:

题目

D. Destroying Roads

time limit per test 2 seconds

memory limit per test 256 megabytes

inputstandard input

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

输入

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

输出

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

样例

input

5 4

1 2

2 3

3 4

4 5

1 3 2

3 5 2

output

0

题意

给你一个无向图,要求从s1到t1的距离要小于等于l1从s2到t2的距离小于等于l2

问你能删除最多多少条边。

题解

首先跑任意两点之间的最短路。

如果两条路完全没有公共边,则答案为m-d[s1][t1]-d[s2][t2]。

如果两条边有公共边,则路径一定为“H"形状的,我们可以通过枚举公共边集的两个端点i,j,暴力所有的情况。

官方题解

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
using namespace std; const int maxn = 3e3+10;
int n, m; vector<int> G[maxn];
int d[maxn][maxn];
int inq[maxn];
void spfa(int s) {
memset(inq, 0, sizeof(inq));
queue<int> Q;
d[s][s] = 0, inq[s] = 1, Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (d[s][v] > d[s][u] + 1) {
d[s][v] = d[s][u] + 1;
if (!inq[v]) inq[v] = 1, Q.push(v);
}
}
}
} void init() {
memset(d, 0x3f, sizeof(d));
} int main() {
scanf("%d%d", &n, &m);
init();
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v), u--,v--;
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 0; i < n; i++) {
spfa(i);
}
int s1, t1, l1, s2, t2, l2;
scanf("%d%d%d", &s1, &t1, &l1),s1--,t1--;
scanf("%d%d%d", &s2, &t2, &l2),s2--,t2--;
if (d[s1][t1]>l1 || d[s2][t2]>l2) { printf("-1\n"); return 0;
}
int ans = d[s1][t1] + d[s2][t2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
if (d[s1][i] + d[i][j] + d[j][t1] <= l1&&d[s2][i] + d[i][j] + d[j][t2] <= l2) {
ans = min(ans, d[s1][i] + d[s2][i] + d[i][j] + d[j][t1] + d[j][t2]);
}
if (d[s1][i] + d[i][j] + d[j][t1] <= l1&&d[s2][j] + d[j][i] + d[i][t2] <= l2) {
ans = min(ans, d[s1][i] + d[i][j] + d[j][t1] + d[s2][j] + d[i][t2]);
}
}
}
printf("%d\n", m - ans);
return 0;
}

Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路的更多相关文章

  1. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路 删边

    题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cs ...

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

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

  4. 完全背包 Codeforces Round #302 (Div. 2) C Writing Code

    题目传送门 /* 题意:n个程序员,每个人每行写a[i]个bug,现在写m行,最多出现b个bug,问可能的方案有几个 完全背包:dp[i][j][k] 表示i个人,j行,k个bug dp[0][0][ ...

  5. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...

  6. 水题 Codeforces Round #302 (Div. 2) A Set of Strings

    题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...

  7. Codeforces Round #302 (Div. 2)

    A. Set of Strings 题意:能否把一个字符串划分为n段,且每段第一个字母都不相同? 思路:判断字符串中出现的字符种数,然后划分即可. #include<iostream> # ...

  8. Codeforces Round #369 (Div. 2) D. Directed Roads 数学

    D. Directed Roads 题目连接: http://www.codeforces.com/contest/711/problem/D Description ZS the Coder and ...

  9. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

随机推荐

  1. DataGridView 操作

    //dataGridView 删除选中行 int num = dataGridView2.SelectedRows.Count; ) { DataGridViewRow r = dataGridVie ...

  2. blogs

    http://blogs.msdn.com/b/tess/archive/2008/02/04/net-debugging-demos-information-and-setup-instructio ...

  3. IIS7下swfupload上传大文件出现404错误

    要求上传附件大小限制在2G,原本以为可以轻松搞定.在编译模式下可以上传大文件,可是在IIS7下(自己架的服务器),一上传大的文件就会出现 Http 404错误,偶尔有的文件还有IO. error错误. ...

  4. iOS - SWift3 & XCode8

    1. 使用资源文件夹导入并管理图片素材 /* *资源文件夹可以方便您进行图片管理,在读取图片时,不需要加上图片名的后缀.同时还可以提高软件的安全性,它会讲图片都加密压缩, *并保存到   Assets ...

  5. OC11_真正的代理

    // // ReceiveReportDelegate.h // OC11_真正的代理 // // Created by zhangxueming on 15/6/24. // Copyright ( ...

  6. V2EX社区

    无论你是在大学进行人生最重要阶段的学习,或者是在中国的某座城市工作,或者是在外太空的某个天体如 Sputnik 1 上享受人生,在注册进入 V2EX 之后,你都可以为自己设置一个所在地,从而找到更多和 ...

  7. C#简单实现发送手机短信

    偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2) ...

  8. laravel--为什么属性在模型中没有定义,却取出来了值,这些属性哪里来的

    看laravel模型中的这段代码, public function getLimitUsersAttribute() { return $this->user_limit - $this-> ...

  9. Jquery操作Cookie取值错误的解决方法

    使用JQuery操作cookie时 发生取的值不正确,结果发现cookie有四个不同的属性,分享下错误的原因及解决方法. 使用JQuery操作cookie时 发生取的值不正确的问题:  结果发现coo ...

  10. DataSnap数据库连接池,数据集对象池的应用

    传统的应用服务器的开发往往是在ServerMethods单元中拖放一堆TDataSet, TDaTaSetProvider控件,这是一个最简单粗暴的开发方向,往往会造成服务端程序文件的臃肿.服务运行期 ...