题意:

  给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2。


Solution:

  首先可以分两种情况讨论:

    1:假设最后留下的边是互不相交的两条路径。此时的答案是n-s1到t1的最短路径-s2到t2的最短路径。

2:假设最后留下的边有重合的一段,此时只要枚举重合的这一段的起点和终点,就可以判断。注意此时要考虑这两条路径经过枚举的两点的顺序。

限制的条件比较多,可以用来剪枝的条件也很多。

由于所有边的长度为1,用DFS或者SPFA算法可以很快地求出最短路。

#include <bits/stdc++.h>
using namespace std; const int N = ; struct edge {
int v, ne;
} E[N * N << ];
int head[N], cnt; int n, m, ans;
int s1, s2, t1, t2, l1, l2; int dis[][N];
int vis[N]; void SPFA (int S, int k) {
queue<int> q;
memset (vis, , sizeof vis);
dis[k][S] = ;
vis[S] = ;
q.push (S);
while (!q.empty() ) {
int u = q.front(); q.pop();
for (int i = head[u]; i; i = E[i].ne) {
int v = E[i].v;
if (dis[k][u] + < dis[k][v]) {
dis[k][v] = dis[k][u] + ;
if (!vis[v]) {
vis[v] = ;
q.push (v);
}
}
}
vis[u] = ;
}
} inline void add (int u, int v) {
E[++cnt].v = v, E[cnt].ne = head[u];
head[u] = cnt;
} int main() {
ios::sync_with_stdio ();
cin >> n >> m;
for (int i = , u, v; i <= m; ++i) {
cin >> u >> v;
add (u, v), add (v, u);
}
cin >> s1 >> t1 >> l1;
cin >> s2 >> t2 >> l2;
memset (dis, 0x1f, sizeof dis);
SPFA (s1, );
SPFA (s2, );
SPFA (t1, );
SPFA (t2, );
if (dis[][t1] <= l1 && dis[][t2] <= l2) {
ans = dis[][t1] + dis[][t2];
for (int i = ; i <= n; ++i) {
int flag = ;
for (int k = ; k <= n; ++k) dis[][k] = ;
for (int j = i + ; j <= n; ++j) {
int tem1 = min (dis[][i] + dis[][j], dis[][j] + dis[][i]);
int tem2 = min (dis[][i] + dis[][j], dis[][j] + dis[][i]);
if (tem1 + tem2 < ans && tem1 < l1 && tem2 < l2) {
if (!flag) {
SPFA (i, );
flag = ;
}
if (tem1 + dis[][j] <= l1 && tem2 + dis[][j] <= l2) {
ans = min (ans, tem1 + tem2 + dis[][j]);
}
}
}
}
}
else {
ans = m + ;
}
cout << m - ans << endl;
}

Codeforces 543B Destroying Roads(最短路)的更多相关文章

  1. [CodeForces] 543B Destroying Roads

    脑洞+暴力. 因为边权是1,所以bfs一下,O(n^2)求任意两点间最短路,再枚举. ans最大是\(dis_{s1,t1}+dis_{s2,t2}\) 再考虑有公共边的情况,一定存在两个点 u, v ...

  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. CF Destroying Roads (最短路)

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

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

    题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cs ...

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

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

  6. Codeforces 543.B Destroying Roads

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

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

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

  8. [CF544D]Destroying Roads_最短路_bfs

    D. Destroying Roads 题目大意: In some country there are exactly n cities and m bidirectional roads conne ...

  9. B. Destroying Roads

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

随机推荐

  1. Performance Optimization (2)

    DesktopGood performance is critical to the success of many games. Below are some simple guidelines f ...

  2. Hash(4) hashtable,hashmap

    首先,我们要知道set是利使用map是实现的,因为只要利用map中的key唯一性就行了. 1.hashmap 和hashtable的区别是什么? 我们可以背出:  hashtable线程安全.hash ...

  3. python 3 操作 excel

    看到一篇很好的python读写excel方式的对比文章: 用Python读写Excel文件 关于其他版本的excel,可以通过他提供的链接教程进行学习. XlsxWriter: https://git ...

  4. 如何选择NoSql数据库

    How to choose a No Sql database 介绍了一下怎么选择一个No Sql数据库,下面简单翻译一下重点. No Sql的数据库可以分为如下4类: Key-Value数据库 数据 ...

  5. 圣诞节来了,雪花纷飞的CSS3动画

    原文链接:http://www.html5think.com/article/index/id/80

  6. 学习NodeJS第一天:node.js引言

    Node.JS 是资深 C 程序猿 Ryan Dahl(http://four.livejournal.com/)的作品,根据 Google 著名的开源 JavaScript 引擎 V8 来进行二次开 ...

  7. poj1066

    很好的一道题.题意是,一个正方形围墙内有一些交错的内墙,内墙的端点都在正方形上,在正方形内部有一个点,求从正方形外到这个点的最少要走的门数,门只能是线段的中点. 思路很巧妙,因为从一个点到终点不可能“ ...

  8. iOS-系统定位功能

    ios系统定位 前期准备 系统定位功能,需要用到框架:CoreLocation/CoreLocation.h, 然后导入文件#import <CoreLocation/CoreLocation. ...

  9. [转] How to dispatch a Redux action with a timeout?

    How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...

  10. css考核点整理(八)-在什么情况下通过img引入图片,什么情况用背景图引入?背景属性有哪些

    在什么情况下通过img引入图片,什么情况用背景图引入?背景属性有哪些