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 ...
随机推荐
- HTML+CSS学习笔记(9)- CSS的继承、层叠和特殊性
标签:HTML+CSS 继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这 ...
- UILabel自适应高、宽
根据Label和字体大小自适应高度 - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.n ...
- asp.net visio com接口 asp.net和visio混合编程
主要介绍asp.net调用visio com的基本用法,主要用于控制visio图形背景色,文字显示等. 主要步骤: 1. 项目中引用COM组件,找到Mircosoft Visio 14 Type Li ...
- HDU1042 N! 大数的阶乘
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 由于数字特别大, 所以用数组来模拟. 经测试, 数组大小开50000 可过. 附AC代码, 欢迎 ...
- Archiving
There are typically four steps of archving: Preprocessing Write Store Delete Normally Store is inv ...
- <postfix邮件服务下mysql的升级>
本片服务的环境的红帽的企业版6.5 的,6.3的测试可能会略有不一样,不过方法大致是一样的. 当前系统的postfix的版本为 postfix-2.6.6-2.2.el6_1.x86_64 我们要向使 ...
- 字符串反转(charat)
package com.java1234.chap03.sec08; public class zifufanzhuan { public static void main(String[] args ...
- WCF 服务与终结点(四)
服务 服务是一组公开功能的集合. 服务内部包含了如语言.技术.版本与框架等概念,服务之间的交互只允许使用规定的通信模式 外界客户端并不知道服务内部的实现细节,所以WCF服务通常通过元数据的方式描述可用 ...
- Bootstrap模态框
backdrop选项,当设置成false的时候, 背景不会出现半透明的遮盖层,当用户点击模态框外部时不会关闭模态框: 设置成true的时候会出现遮盖层,当用户点击模态框外部时则会关闭模态框. 那如果又 ...
- Linux大量TIME_WAIT的解决办法
发布:theboy 来源:net [大 中 小] 根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方 socket将进入TIME_WAIT状态,TIME_WAIT状态将持 ...