Codeforces 543.B Destroying Roads
2 seconds
256 megabytes
standard input
standard output
In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.
You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.
Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.
The first line contains two integers n, m (1 ≤ n ≤ 3000,
) — the number of cities and roads in the country, respectively.
Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.
The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n).
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
0
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
1
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
-1
题目大意:给定一张边权均为1的无向图。 问至少需要保留多少边,使得s1到t1的最短路不超过l1,s2到t2的最短路不超过l2。
分析:其实就是求最后s1到t1最短路和s2到t2最短路的路径并嘛.
画几个图会发现最后路径的形式一定是分叉的四段加上重叠的一段(每一段都可能为空).只需要保留这些边,也就是保证了只有一条路可达.那么枚举这个重叠部分的两端,计算一下两端分别到s1,t1,s2,t2的最短路,最后更新答案即可.这一步可以预处理得到.
坑点:重叠部分可能为一个点;做一次后s1,t1要交换!
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 0x7ffffff;
int n,m,head[maxn],to[maxn * maxn],nextt[maxn * maxn],tot = ,d[maxn][maxn],vis[maxn];
int s1,t1,l1,s2,t2,l2,ans; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void bfs(int x)
{
for (int i = ; i <= n; i++)
d[x][i] = inf;
memset(vis,,sizeof(vis));
queue <int> q;
q.push(x);
vis[x] = ;
d[x][x] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (d[x][v] > d[x][u] + )
{
d[x][v] = d[x][u] + ;
if (!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for (int i = ; i <= m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for (int i = ; 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)
puts("-1");
else
{
ans = d[s1][t1] + d[s2][t2];
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (d[i][j] >= inf)
continue;
int temp1 = d[s1][i] + d[i][j] + d[j][t1];
int temp2 = d[s2][i] + d[i][j] + d[j][t2];
if (temp1 > l1 || temp2 > l2)
continue;
int temp = temp1 + temp2 - d[i][j];
ans = min(temp,ans);
}
}
swap(s1,t1);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (d[i][j] >= inf)
continue;
int temp1 = d[s1][i] + d[i][j] + d[j][t1];
int temp2 = d[s2][i] + d[i][j] + d[j][t2];
if (temp1 > l1 || temp2 > l2)
continue;
int temp = temp1 + temp2 - d[i][j];
ans = min(temp,ans);
}
}
printf("%d\n",m - ans);
} return ;
}
Codeforces 543.B Destroying Roads的更多相关文章
- codeforces 544 D Destroying Roads 【最短路】
题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...
- Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路
D - Destroying Roads Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...
- 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 543 B. World Tour
http://codeforces.com/problemset/problem/543/B 题意: 给定一张边权均为1的无向图. 问至多可以删除多少边,使得s1到t1的最短路不超过l1,s2到t2的 ...
- CF Destroying Roads (最短路)
Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- Codeforces 806 D.Prishable Roads
Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...
- [CF544] D. Destroying Roads
D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 如何在 Debian 9 下安装 LEMP 和 WHMCS 7.5
WHMCS 7.5 发布了,它开始支持 PHP 7.2,这里就写个简单的教程记录一下安装方式. 1.准备工作 首先,我们需要按照 在Debian 9 / Debian 8 下使用源安装方式安装 LEM ...
- Phonegap 环境配置
目前要开发 Web App 还是有比较多的选择的 如 Phonegap.MUI.AppCan,接下来以 Web前端开发工程师 的角度来一个 Phonegap 的 First Blood 一.开发环境: ...
- 《Linux内核与分析》第五周
20135130王川东 一.给MenuOS增加time和time-asm命令 命令:1.强制删除:rm menu -rf 2.克隆:git clone (后跟需要克隆数据所在的位置) 3.自动编译,自 ...
- struts通配符*的使用
<action name="user_*" class="com.wangcf.UserAction" method="{1}"> ...
- Requests库与HTTP协议
了解HTTP协议 请求与响应模式的协议: 用户提出对URL(用来定位网络中的资源位置)地址数据的操作请求,服务器给予相应. 无状态的应用层协议:两次请求之间不会互相影响. HTTP协议支持的请求种类: ...
- lintcode-208-赋值运算符重载
208-赋值运算符重载 实现赋值运算符重载函数,确保: 新的数据可准确地被复制 旧的数据可准确地删除/释放 可进行 A = B = C 赋值 说明 本题只适用于C++,因为 Java 和 Python ...
- jdbc 6.0
1.获取数据库自动生成的键值 package com.rong.jielong; import java.sql.Connection; import java.sql.DriverManager; ...
- php对二维数组排序
function my_sort($arrays,$sort_key,$sort_order=SORT_DESC,$sort_type=SORT_NUMERIC ){ if(is_array($arr ...
- 微信小程序组件 加减号弹出框
<!-- 点击立即抢拼弹出框 --> <view class='add-rob' bindtap="setModalStatus" data-status=&qu ...
- mysql中一些表选项
表选项列表 表选项就是,创建一个表的时候,对该表的整体设定,主要有如下几个: charset = 要使用的字符编码, engine = 要使用的存储引擎(也叫表类型), auto_increment ...