CF543B Destroying Roads 枚举 + 思维 + BFS
Code:
#include <bits/stdc++.h>
#define ll long long
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 3002
using namespace std;
queue<int>Q;
vector<int>G[maxn];
int n,m,s1,t1,l1,s2,t2,l2;
int vis[maxn],d[maxn][maxn];
void bfs(int s) {
memset(vis,0,sizeof(vis));
d[s][s]=0,vis[s]=1,Q.push(s);
while(!Q.empty()) {
int u=Q.front(); Q.pop();
for(int i=0;i<G[u].size();++i) {
int v=G[u][i];
if(!vis[v]) {
vis[v]=1,d[s][v]=d[s][u]+1;
Q.push(v);
}
}
}
}
int main() {
// setIO("input");
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i) {
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b), G[b].push_back(a);
}
memset(d,0x3f,sizeof(d));
for(int i=1;i<=n;++i) d[i][i]=0;
for(int i=1;i<=n;++i) bfs(i);
scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2);
if(d[s1][t1] > l1 || d[s2][t2] > l2) printf("-1\n");
else {
int ans=d[s1][t1] + d[s2][t2];
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j) {
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]);
}
}
swap(s1,t1);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j) {
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]);
}
}
printf("%d\n",m-ans);
}
return 0;
}
CF543B Destroying Roads 枚举 + 思维 + BFS的更多相关文章
- CF543B Destroying Roads 题解
看到没有题解就贡献一波呗 分析: 这题其实就是想让我们求一个图中两条最短路的最短(好把更多的边删掉). 我们先考虑一条最短路,别问我我怎么会的显然,就是s和t跑个最短路再用n-就行. 然后就是两条喽! ...
- Codeforces 543.B Destroying Roads
B. 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\) ...
- 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 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 ...
- 危险系数(枚举点+bfs)--------蓝桥备战系列
标题:危险系数 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定 ...
随机推荐
- ARTS挑战
最近有点迷茫,感觉自己工作了一年多,技术成长有限,我要做出改变.2019年11月2日,就从今天开始,参加耗子叔的ARTS挑战. ARTS的初衷 Algorithm:主要是为了编程训练和学习.每周至少做 ...
- PIL实现图片框架以及生成图片验证码
1. PIL的基本概念 PIL:即Python Imaging Library,是Python平台事实上的图像处理标准库. PIL中涉及通道(bands)和模式(mode)这两个重要概念. (1)通道 ...
- realloc ------ 扩大malloc得到的内存空间
char* p = malloc(1024);char* q = realloc(p,2048); 现在的问题是我们应该如何处理指针 p. 刚开始按照我最直观的理解,如果就是直接将 p = NULL; ...
- redis在微博与微信等互联网应用笔记
Redis实战redis在微博与微信等互联网应用实例讲解全集 1. 对象缓存 id user balance 1 john 1200 2 tom 3000 对于这种存储,redis可以使用mset o ...
- linux-memcache安装及memcached memcache扩展
linux memcached安装yum -y install libevent libevent-deve yum list memcached yum -y install memcached m ...
- vue-split-table【表格合并和编辑插件】
前言 vue-split-table应用的效果图 vue-split-table开源地址,欢迎star,现在已经开源和同步到npm上轻松搞定表格拆分或者合并,编辑,再也不怕被产品怼啦 1.核心源码分析 ...
- [项目实战]训练retinanet(pytorch版)
采用github上star比较高的一个开源实现https://github.com/yhenon/pytorch-retinanet 在anaconda中新建了一个环境,因为一开始并没有新建环境,在原 ...
- 剑指offer-二叉树的镜像-python
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- react面试题——理解setState(源码object.assign)
setState是异步的方式 this.setState({ counter:this.state.counter+1 }) console.log(this.state.counter) s ...
- Python性能分析工具Profile
Python性能分析工具Profile 代码优化的前提是需要了解性能瓶颈在什么地方,程序运行的主要时间是消耗在哪里,对于比较复杂的代码可以借助一些工具来定位,python 内置了丰富的性能分析工具,如 ...