Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
题目链接:
题目
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 最短路的更多相关文章
- Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路 删边
题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cs ...
- Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路
D - Destroying Roads Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...
- Codeforces Round #302 (Div. 1) B - Destroying Roads
B - Destroying Roads 思路:这么菜的题我居然想了40分钟... n^2枚举两个交汇点,点与点之间肯定都跑最短路,取最小值. #include<bits/stdc++.h> ...
- 完全背包 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][ ...
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
- Codeforces Round #302 (Div. 2)
A. Set of Strings 题意:能否把一个字符串划分为n段,且每段第一个字母都不相同? 思路:判断字符串中出现的字符种数,然后划分即可. #include<iostream> # ...
- Codeforces Round #369 (Div. 2) D. Directed Roads 数学
D. Directed Roads 题目连接: http://www.codeforces.com/contest/711/problem/D Description ZS the Coder and ...
- 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 ...
随机推荐
- Part 8 AngularJS filters
Filters in angular can do 3 different things 1. Format data 2. Sort data 3. Filter data Filters can ...
- Android之动态图片
在Android中,比起静态图片来动态图片会更加生动更加酷炫,因为这种视觉效果,你应该会发现我们手机中大多数应用软件的导航页面也都是采用动态图片来展示.动态图片的格式有gif.png格式等等. 我们就 ...
- 初识iOS9 iPad新特性SlideView和SplitView的适配
苹果刚发布了iOS9,在iPad上新增了两个新的特性SlideView和SplitView,前者可以在不关闭当前激活APP的情况下调出来另外个APP以30%比例显示进行操作使用,后者允许同时运行两个A ...
- 密码强度的js插件(完成)
效果如下图: 低:
- 修改ubuntu按下关机键触发的事件
gsettings set org.gnome.settings-daemon.plugins.power button-power shutdown will change your the beh ...
- 把url后面的.html去掉
$url = "http://haichuang1997.bmlink.com/supply/dc_355472.html";echo rtrim($url, '.html');
- IntellijIDEA 使用技巧
1:显示工具栏目 toolbar:view ->ToolBar 2:加载源码 new project ->选择java project ->选择源码所在目录 ->ok
- Jquery插件收集
移动端滚动条插件iScroll.js http://www.cnblogs.com/starof/p/5215845.html http://www.codeceo.com/article/35-jq ...
- PyQt4学习笔记1:PyQt4第一个程序
创建一个 PyQt4 一般可以通过很少的步骤完成.通常的方法是用Qt 提供的QtDesigner工具创建界面.使用QtDesigner,可以方便地创建复杂的GUI界面.然后,可以在窗口上创建部件, 添 ...
- PowerPoint Office Mix 插件
一个内嵌在PowerPoint里的一个教学工具,可以以PPT为核心录制视频. 点下面链接了解并安装 https://mix.office.com/ 首先这货是免费,当然是基于PowerPoint的基础 ...