[CF544D]Destroying Roads_最短路_bfs
D. Destroying Roads
题目大意:
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).
题解:
首先,保证了删掉的边最多,那就说明$s1$到$t1$和$s2$到$t2$都分别只有一条路径,不然的话我们还可以删掉更多的边。
接下来我们考虑,最终答案的形式。
必定是如下三种情况之一:
第一种,这两条路径互不相交。就是$s1$到$t1$,$s2$到$t2$。
第二种,存在一条公共路径,$l$到$r$,答案是$s1$到$l$,$l$到$r$,$r$到$t1$;和$s2$到$l$,$l$到$r$,$r$到$t2$。
最后一种是$s2$和$t2$调换,也就是$t2$到$l$,$l$到$r$,$r$到$s2$。
显然,每段路径都是最短路。
我们需要枚举$l$和$r$,也就是说我们需要多源最短路。
但是已知的算法最快也只能做到$n^2logn$,跑$n$遍堆优化$Dijkstra$。
好慢啊.....
诶,我们发现每条边的边权都相等,所以我们可以直接$bfs$。
因为边权都相等,所以每个点第一次到的时间戳就是距离。
然后枚举更新答案就好,不要忘记了第一种情况和判断是否超出了长度上限$l1$和$l2$。
代码:
#include <bits/stdc++.h> #define N 3010 using namespace std; int head[N], to[N << 1], nxt[N << 1], tot; int dis[N][N]; bool vis[N]; queue<int > q; char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000,stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} inline void add(int x, int y) {
to[ ++ tot] = y;
nxt[tot] = head[x];
head[x] = tot;
} void bfs(int x) {
while (!q.empty())
q.pop();
memset(dis[x], 0x3f, sizeof dis[x]);
memset(vis, false, sizeof vis);
vis[x] = true;
dis[x][x] = 0;
q.push(x);
while (!q.empty()) {
int p = q.front(); q.pop();
for (int i = head[p]; i; i = nxt[i]) {
if (!vis[to[i]]) {
dis[x][to[i]] = dis[x][p] + 1;
vis[to[i]] = true;
q.push(to[i]);
}
}
}
} int main() {
int n = rd(), m = rd();
for (int i = 1; i <= m; i ++ ) {
int x = rd(), y = rd();
add(x, y), add(y, x);
}
int s1 = rd(), t1 = rd(), l1 = rd();
int s2 = rd(), t2 = rd(), l2 = rd();
for (int i = 1; i <= n; i ++ ) {
bfs(i);
}
if(dis[s1][t1] > l1 || dis[s2][t2] > l2)
puts("-1"), exit(0);
int ans = dis[s1][t1] + dis[s2][t2];
for (int i = 1; i <= n ; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
int v1, v2;
v1 = dis[s1][i] + dis[i][j] + dis[j][t1];
v2 = dis[s2][i] + dis[i][j] + dis[j][t2];
if(v1 <= l1 && v2 <= l2)
ans = min(ans, v1 + v2 - dis[i][j]);
v2 = dis[s2][j] + dis[j][i] + dis[i][t2];
if(v1 <= l1 && v2 <= l2)
ans = min(ans, v1 + v2 - dis[i][j]);
}
}
printf("%d\n", m - ans);
return 0;
}
小结:好题啊。对于一个没有思路的题,我们可以想一想最终答案的样子。如果有没有用上的条件,看看能不能通过那个条件来优化当前的不完美算法。
[CF544D]Destroying Roads_最短路_bfs的更多相关文章
- CF Destroying Roads (最短路)
Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- 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 ...
- POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索
题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...
- codeforces 544 D Destroying Roads 【最短路】
题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...
- 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
- Codeforces 543.B Destroying Roads
B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- MFC 标签页Tab Control
自带的标签页不好用,因此借助了TabSheet文件TabSheet源码 1.在解决方案资源管理器——项目处鼠标右键——在文件资源管理器中打开文件夹(或者按下图,更方便),把TabSheet.h.Tab ...
- children([expr]) 取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合。
children([expr]) 概述 取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合. 可以通过可选的表达式来过滤所匹配的子元素.注意:parents()将查找所有祖辈元素,而chil ...
- virtualBox中有线和无线两种情况下centos虚拟机和本地机互ping的方案
之前写微信点餐系统的时候,刚开始是无线连接,然后每次进去虚拟机ip和本地ip都会改变,所以每次都需要配置一下nginx,还有本地的路径.之后换有线连接,就研究了一下桥接模式有线情况下虚拟机静态ip设置 ...
- 路由器配置——OSPF协议(1)
一.实验目的:用OSPF协议使全网互通 二.拓扑图 三.具体步骤配置 (1)R1路由器配置 Router>enableRouter#configure terminalEnter configu ...
- 51 Nod 1509 加长棒(巧妙的隔板法计数)
1509 加长棒 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 现在有三根木棒,他们的长度分别是a,b,c厘米 ...
- CSP-S2019游记——终点
准退役一年了,回来苟CSP,填补去年留下的遗憾,也算是为这个不那么完美的高中OI生涯划一个句点吧. DAY 1 考前:昨天晚上睡得不太好.早上洛谷打卡居然是中吉(3K说的大吉嘞???).在地铁上有点犯 ...
- bbs项目---表关系
表关系 用户表个人博客表点赞表文章表文章描述表文章和标签多对多关系表评论表分类表标签表 表关系设计示例收集: 1 https://bbs.csdn.net/topics/390260474 2 上图博 ...
- jenkins+Maven从SVN上构建项目
一.安装Maven 下载地址:https://maven.apache.org/download.cgi 把下载的安装包解压 tar -xvf apache-maven--bin.tar.gz 配置环 ...
- CF883H
CF883H 题意: 给你一个字符串,需要把它以最小的划分次数划分为x个长度相等的回文串,可以重新排列. 解法: 大模拟一个. 分别统计出现一次的字符和出现两次的字符,如果没有出现一次的字符,那么所有 ...
- 关于php文件操作的几个小trick
记录一些ctf题目中近期遇到的一些文件操作trick,不定时更新 1.move_uploaded_file 一般用来保存上传的文件,第二个参数一般是最终保存的文件名,针对此函数,若在一定条件下$new ...