Codeforces543 B. Destroying Roads
传送门:>Here<
题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, dis(s2,r2)<=l2
解题思路
首先我们会发现,由于边权都为1,删去一些边,某两点间的最短路肯定会随着删的边越来越多而越来越长(捷径被删了)
因此我们会发现,要让边删的尽量多,最好最后只剩下最短路,其它边都剩下。换句话说,如果两条最短路不相交,那么最后只会剩下孤零零的两条链
于是我们会发现,我们可以初步得到一个答案:$M - d(s1,t1) - d(s2,t2)$
但这有可能不是最优的答案——两条最短路有可能有重叠。重叠的部分越长,答案就会越大也就是更优。并且很容易证明,重叠肯定只有连续的一段,而不会有间断的两断——因为如果需要有两断的话肯定不如连起来变为一段来的优。所以可以$O(n^2)$枚举重叠部分,验证一下就好了
Code
用SPFA预处理出任意两点间的距离,注意数组要开$N*N$
教训:当出现莫名其妙的错误的时候,首先检查数组有没有开错——例如,int开成bool,开得太小等等
/*By QiXingzhi*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int MAXM = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar(); return x * w;
}
int N,M,x,y,s1,t1,l1,s2,t2,l2,top,sp1;
int first[MAXM*],nxt[MAXM*],to[MAXM*],num_edge;
int d[MAXN][MAXN],inQ[MAXN],pre[MAXN],ans[MAXN],tmp[MAXN];
queue <int> q;
inline void add(int u, int v){
to[++num_edge] = v;
nxt[num_edge] = first[u];
first[u] = num_edge;
}
inline void SPFA(int s, int c){
for(int i = ; i <= N; ++i) d[i][c] = INF;
memset(inQ, , sizeof(inQ));
d[s][c] = ;
q.push(s);
int u,v;
while(!q.empty()){
u = q.front();q.pop();
inQ[u] = ;
for(int i = first[u]; i; i = nxt[i]){
v = to[i];
if(d[u][c] + < d[v][c]){
d[v][c] = d[u][c] + ;
if(!inQ[v]){
inQ[v] = ;
q.push(v);
}
}
}
}
}
int main(){
// freopen(".in","r",stdin);
N=r,M=r;
for(int i = ; i <= M; ++i){
x=r,y=r;
add(x, y), add(y, x);
}
s1=r,t1=r,l1=r;
s2=r,t2=r,l2=r;
for(int i = ; i <= N; ++i){
SPFA(i, i);
}
if(d[s1][t1] > l1 || d[s2][t2] > l2){ printf("-1"); return ; }
int Ans = M - d[s1][t1] - d[s2][t2];
if(Ans < ) Ans = ;
for(int i = ; i <= N; ++i){
for(int j = ; j <= N; ++j){
if(i == j) continue;
if(d[s1][i] + d[j][t1] + d[i][j] <= l1){
if(d[s2][i] + d[j][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[j][t2]));
}
if(d[s2][j] + d[i][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][i]+d[i][j]+d[j][t1]+d[s2][j]+d[i][t2]));
}
}
if(d[s1][j] + d[i][t1] + d[i][j] <= l1){
if(d[s2][i] + d[j][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][j]+d[i][j]+d[i][t1]+d[s2][i]+d[j][t2]));
}
if(d[s2][j] + d[i][t2] + d[i][j] <= l2){
Ans = Max(Ans, M - (d[s1][j]+d[i][j]+d[i][t1]+d[s2][j]+d[i][t2]));
}
}
}
}
printf("%d", Ans);
return ;
}
Codeforces543 B. Destroying Roads的更多相关文章
- 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 543.B Destroying Roads
B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #302 (Div. 1) B - Destroying Roads
B - Destroying Roads 思路:这么菜的题我居然想了40分钟... n^2枚举两个交汇点,点与点之间肯定都跑最短路,取最小值. #include<bits/stdc++.h> ...
- Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路
D - Destroying Roads Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...
- [CF544] D. Destroying Roads
D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- B. Destroying Roads
Destroying Roads 题目链接 题意 n个点,m条边每两个点之间不会有两个相同的边,然后给你两个起s1,s2和终点t1,t2; 求删除最多的边后满足两个s1到t1距离\(<=l1\) ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- [CodeForces] 543B Destroying Roads
脑洞+暴力. 因为边权是1,所以bfs一下,O(n^2)求任意两点间最短路,再枚举. ans最大是\(dis_{s1,t1}+dis_{s2,t2}\) 再考虑有公共边的情况,一定存在两个点 u, v ...
随机推荐
- A4纸尺寸 web打印报告
A4纸对应的像素尺寸: <style> @media print { .Noprn{ display:none;} .print-hidden { display: none !impor ...
- C++入门之初话多态与虚函数
多态性是面向对象程序设计的又一个重要思想,关于多态的详尽描述,请看本人的收藏https://www.cnblogs.com/hust-ghtao/p/3512461.html.这篇博文中,详尽的探讨了 ...
- 砝码组合(dfs)
砝码组合 题目内容:用天平称重时,我们希望用尽可能少的砝码组合称出尽可能多的重量.如果只有5个砝码,重量分别是1,3,9,27,81.则它们可以组合称出1到121之间任意整数重量(砝码允许放在左右两 ...
- 【学习总结】C-翁恺老师-入门-第0周<程序设计与C>
[学习总结]C-翁恺老师-入门-总 1-首先按视频说的下载编辑器 <DevC++> 并一路默认设置: 安装包下载链接 (我有vc6.0不过预感告诉我老师要用类似CS50里那种命令行编辑器? ...
- 解决ERR Client sent AUTH, but no password is set
在搭建cookies池时,需要将账号密码保存到redis,保存时报错:ERR Client sent AUTH, but no password is set 报错原因:Redis服务器没有设置密码, ...
- Squid配置之使用帐号密码验证
转自: https://blog.csdn.net/atco/article/details/43448885 1.安装squid使用root用户进行操作.先使用rpm检测是否已经安装了sql ...
- linux的一些基本命令
一.linux的一些基本命令(使用的是CentOS7系统): 1.创建用户组,创建新用户并添加到用户组 添加用户,添加用户组命令: 增加用户:useradd -d /usr/username -m u ...
- vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址
一.开发环境中跨域 使用 Vue-cli 创建的项目,开发地址是 localhost:8080,需要访问非本机上的接口http://10.1.0.34:8000/queryRole.不同域名之间的访问 ...
- python学习笔记(5-1)-基本数据类型-字符串类型及操作
五.字符串处理函数 len(x):字符串x的长度.如len("12345")结果为5 str(x):任意类型x所对应的字符串形式. >>> str(123) ...
- wget 下载网页
如有转载,不胜荣幸.http://www.cnblogs.com/aaron-agu/ wget --http-user=username --http-passwd=password http:/w ...