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

分析:

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

我们先考虑一条最短路,别问我我怎么会的显然,就是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中String 转为Path Data

    定义方法: Geometry PathMarkupToGeometry(string pathMarkup) { string xaml = "<Path " + " ...

  2. Java 几个有用的命令 - All Options, Memory Options, GC Options, System Properties, Thread Dump, Heap Dump

    jcmd  ##Refer to http://www.cnblogs.com/tang88seng/p/4497725.html java -XX:+PrintFlagsFinal -version ...

  3. Linux下C/C++帮助手册安装方法

    1.  安装C的帮助手册 如果你使用的Linux发行版, 默认没有安装C语言的库函数MAN手册, 使用下面的方法解决: # sudo apt-get install manpages # sudo a ...

  4. Delphi xe5调用外部扫描程序——谷歌 zxing

    unit uZXing; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Va ...

  5. Spring5源码深度分析(二)之理解@Conditional,@Import注解

    代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...

  6. 使用豆瓣的pip源安装python模块

    1.指定豆瓣pip源安装Django pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com ...

  7. 记一次腾讯IEG面试失败经历

    如果这是一次成功的经历,估计浏览量不会低.无奈本人能力有限,而且一直在实习,准备时间与面试经验有限导致此次失败,不过,失败也是一种宝贵的经验,我希望也相信这里能给大家一些比较珍贵的经验,废话不多说,上 ...

  8. 使用事件注册器进行swoole代码封装

    在使用swoole的时候,事件回调很难维护与编写,写起来很乱.特别在封装一些代码的时候,使用这种注册,先注册用户自己定义的,然后注册些默认的事件函数. Server.php class Server ...

  9. spring源码深度解析— IOC 之 自定义标签解析

    概述 之前我们已经介绍了spring中默认标签的解析,解析来我们将分析自定义标签的解析,我们先回顾下自定义标签解析所使用的方法,如下图所示: 我们看到自定义标签的解析是通过BeanDefinition ...

  10. octavia的实现与分析(一)·openstack负载均衡的现状与发展以及lvs,Nginx,Haproxy三种负载均衡机制的基本架构和对比

    [负载均衡] 大量用户发起请求的情况下,服务器负载过高,导致部分请求无法被响应或者及时响应. 负载均衡根据一定的算法将请求分发到不同的后端,保证所有的请求都可以被正常的下发并返回. [主流实现-LVS ...