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 ...
随机推荐
- Jquery实现循环删除Reaper某一行
一.实现的效果图:(点击删除图标,juery实现删除整行) 二.MVC开发模式 SQLServer层 #region 删除 /// <summary> /// 根据自动编号删除快递线路信息 ...
- Exchange之三合一部署
1. 前期准备条件之安装filter包 2. 前期准备之安装组件,命令如下 Add-WindowsFeature NET-Framework,RSAT- ...
- 【原创】如何找到Oracle中哪条记录被锁
通常有这种情况,某个表或者准确的说是表的某条记录被锁(TX锁),在业务层面排查之余,一般都会想知道是哪条记录被锁,每次被锁的是否是同一条记录?还是每次都不同?通过记录可以找到这条记录可以在哪个模块.哪 ...
- GForms开发平台
1. 开发平台概述 1.1. 产品概述 GForms开发平台让开发人员甚至非技术人员在短短几分钟内创建全功能的展现服务,让开发团队更加适应客户和市场的需求,从而提高客户服务和速度实现收益. GForm ...
- Coarse-Grained lock 粗粒度锁
用一个锁Lock一组相关的对象 有时,需要按组来修改多个对象. 这样,在需要锁住其中一个的时候,必须连带地将其他的对象都上锁. 为每一个对象都加上一个锁是很繁琐的. 粗粒度锁是覆盖多个对象的单个锁. ...
- DataGrid实现逻辑分页
在ASP.NET内建提供的所有数据排列控件中,只有DataGrid数据控件是提供数据分页功能的.DataReapter数据控件只能提供一些简单 的.基础的数据重复排列功能,对于一些比较复杂的应用是无能 ...
- CSS3/jQuery创意盒子动画菜单
作为前端开发者,各种各样的jQuery菜单见过不少,这款jQuery/CSS3菜单却是别具一格,菜单项嵌入到九宫格中,像小盒子一样,加上温馨的背景,菜单整体外观十分可爱.点击菜单项,盒子就会展开,展示 ...
- 用委托在listbox中异步显示信息,解决线程间操作无效,从不是创建控件的线程访问它
//创建一个委托,是为访问listbox控件服务的. public delegate void UpdateTxt(string msg); //定义一个委托变量 public UpdateTxt u ...
- Nginx 之并发优化
客户端/服务端 连接数 ulimit -n 100000 nginx 链接数 10240 个 worker_connections 10240;允许打开文件数worker_processes 1;wo ...
- Mod 与 RequireJS/SeaJS 的那些事
本文的目的是为了能大让家更好的认识 Mod,之所以引入 RequireJS/SeaJS 的对比主要是应大家要求更清晰的对比应用场景,并不是为了比较出孰胜孰劣,RequireJS 和 SeaJS 都是模 ...