题目链接:

题目

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. HTML5 Web SQL Database 数据库的使用方法【图文说明】

    页面代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  2. iOS - 移动设备防丢失App

    一.原理 二.数据获取 三.报警

  3. C#代码配置IIS 操纵IIS

    前言: IIS到目前经历了四个版本分别为 IIS4.0  IIS5.0 IIS6.0 IIS7.0,其中IIS6.0 IIS7.0是在5.0的安全问题的基础上获得的发展,目前为止.6.0版本以后的都是 ...

  4. jqure获取单选按钮的值(比如性别)

    使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项: 1. ...

  5. C# 将日期转换成中文格式

    没有什么难点,只是要小心,要考虑到月.日上 10 的说法,比如:10 不能直接转换成一〇,也不能像上 20 那样转换成一十〇,应该是十. 特点总结: 数字为 10 时,结果为十: 数字大于 10 时, ...

  6. 简单的json传送数据

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  7. 南阳理工ACM954--N!

    http://acm.nyist.net/JudgeOnline/problem.php?pid=954 循环的可怕之处!! 所有的测试数据结果完全一样.只是超时!!TimeLimitExceeded ...

  8. Codevs 2833 奇怪的梦境

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还 ...

  9. activiti搭建(二)与Spring集成

    转载请注明源地址:http://www.cnblogs.com/lighten/p/5876773.html 本文主要讲解如何将Activiti和Spring框架集成,再过一段时间将会将一个基础的de ...

  10. wpf窗体中复合控件焦点控制

    1.         自定义控件 在UserControl标记中 <UserControl KeyboardNavigation.ControlTabNavigation="Local ...