看到没有题解就贡献一波呗

分析:

这题其实就是想让我们求一个图中两条最短路的最短(好把更多的边删掉)。

我们先考虑一条最短路,别问我我怎么会的显然,就是s和t跑个最短路再用n-就行。

然后就是两条喽!这不就是做两次吗,我太巨了!

这当然是可以的

——不过只是一种情况

考虑到我们的两条路径可能会有重合,我们只好枚举重合最短路的左右端点,将路径分为5段(6段?)来处理。

然后问题基本就解决了,最开始把两两点之间预处理出最短路,这里要注意bfs即可别告诉我你要用Floyd

最后的答案如果比m大就-1了。

l1,l2的限制条件不要忘了判断鸭!

代码:

#include<cstdio>
#include<queue>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int d[3005][3005];
int vis[3005];
vector<int>e[3005];
void bfs(int s)
{
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
//vis[s]=1;
d[s][s]=0;
while(!q.empty())
{
int x=q.front();q.pop();
if(vis[x])continue;
vis[x]=1;
for(int i=0;i<e[x].size();i++)
{
int y=e[x][i];
if(d[s][y]==-1)
{
d[s][y]=d[s][x]+1;
//printf("%d\n",d[s][y]);
q.push(y);
}
}
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(d,-1,sizeof(d));
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
int s1,t1,l1,s2,t2,l2;
scanf("%d%d%d",&s1,&t1,&l1);
scanf("%d%d%d",&s2,&t2,&l2);
for(int i=1;i<=n;i++)
{
// printf("qaq\n");
bfs(i);
}
/*for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
printf("%d ",d[i][j]);
}
printf("\n");
}*/
int ans=999999999;
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)continue;
if(d[s2][i]+d[i][j]+d[j][t2]>l2)continue;
ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[i][j]+d[j][t2]-d[i][j]);
}
}
if(d[s1][t1]<=l1&&d[s2][t2]<=l2)
{
ans=min(ans,d[s1][t1]+d[s2][t2]);
}
swap(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)continue;
if(d[s2][i]+d[i][j]+d[j][t2]>l2)continue;
ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[i][j]+d[j][t2]-d[i][j]);
}
}
if(d[s1][t1]<=l1&&d[s2][t2]<=l2)
{
ans=min(ans,d[s1][t1]+d[s2][t2]);
}
if(ans>m)printf("-1\n");
else
printf("%d\n",m-ans);
return 0;
}

CF543B Destroying Roads 题解的更多相关文章

  1. CF543B Destroying Roads 枚举 + 思维 + BFS

    Code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s".in", ...

  2. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

    D - Destroying Roads Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...

  4. CF Destroying Roads (最短路)

    Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. Codeforces 543.B Destroying Roads

    B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #302 (Div. 1) B - Destroying Roads

    B - Destroying Roads 思路:这么菜的题我居然想了40分钟... n^2枚举两个交汇点,点与点之间肯定都跑最短路,取最小值. #include<bits/stdc++.h> ...

  7. [CF544] D. Destroying Roads

    D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  9. B. Destroying Roads

    Destroying Roads 题目链接 题意 n个点,m条边每两个点之间不会有两个相同的边,然后给你两个起s1,s2和终点t1,t2; 求删除最多的边后满足两个s1到t1距离\(<=l1\) ...

随机推荐

  1. UWP-MSDN文档分类

    原文:UWP-MSDN文档分类 UWP学习目录整理 0x00 可以忽略的废话 10月6号靠着半听半猜和文字直播的补充看完了微软的秋季新品发布会,信仰充值成功,对UWP的开发十分感兴趣,打算后面找时间学 ...

  2. Qt在Windows上的调试器安装与配置

    如果安装Qt时使用的是Visual Studio的预编译版,那么很有可能就会缺少调试器(Debugger),而使用MSVC的Qt对应的原生调试器是CDB(对应MinGW的Qt使用GDB调试器).本文介 ...

  3. 用 jQuery.getJSON() 跨域请求 JSON 数据

    $.getJSON()可以理解为特殊形式的$.ajax(),手册里的说明好复杂,这里只记录一下用到的跨域请求. 先说在同一域名下,js发送数据到php,php返回JSON数据: $.getJSON(' ...

  4. uva10883_Supermean_数学

    题目大意:给出n个数,每相邻两个数求平均数,得到n-1个数,再求平均数,得到n-2个数,......一直到最后一个数,输出这个数. 题目很简单,就是中间数据会比较大有点复杂,超过double的范围,而 ...

  5. Angular4初学

    [1].在学习Angular4之前,首先要了解一些typescript的知识. 以下是我的总结:https://gitee.com/FangXiaoQi123/angularJSCeShi/blob/ ...

  6. 请你讲一讲JavaScript有哪些数据类型, 数据类型判断有哪些方法?

    js的8种数据类型 JavaScript中有8种数据类型, 包括基本数据类型(number, string, boolean, null, undefined, symbol, bigint)和引用数 ...

  7. 每日一问:浅谈 onAttachedToWindow 和 onDetachedFromWindow

    基本上所有 Android 开发都会接触到 onCreate().onDestory().onStart().onStop() 等这些生命周期方法,但却不是所有人都会去关注到 onAttachXXX( ...

  8. 视频直播技术之iOS端推流

    随着网络基础建设的发展和资费的下降,在这个内容消费升级的时代,文字.图片无法满足人们对视觉的需求,因此视频直播应运而生.承载了实时性Real-Time和交互性的直播云服务是直播覆盖各行各业的新动力.网 ...

  9. OVS实现VXLAN隔离

    一.实验环境 1.准备3个CentOS7 mini版本的虚拟机,每个主机3个网卡.如图: 图中OVS-1.OVS-2.OVS-3分别为三台CentOS7 mini版虚拟机,分别配备3个虚拟网卡.如图中 ...

  10. yii DAO操作总结

    数据库代码: /* Navicat MySQL Data Transfer Source Server         : lonxom Source Server Version : 50524 S ...