B. Destroying Roads
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains two integers nm (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 aibi (1 ≤ ai, bi ≤ nai ≠ 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).

Output

Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

Examples
input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
output
0
input
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
output
1
input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
output
-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的更多相关文章

  1. codeforces 544 D Destroying Roads 【最短路】

    题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...

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

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

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

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

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

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

  5. Codeforces 543 B. World Tour

    http://codeforces.com/problemset/problem/543/B 题意: 给定一张边权均为1的无向图. 问至多可以删除多少边,使得s1到t1的最短路不超过l1,s2到t2的 ...

  6. CF Destroying Roads (最短路)

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

  7. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  8. Codeforces 806 D.Prishable Roads

    Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...

  9. [CF544] D. Destroying Roads

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

随机推荐

  1. 导出Office365中的组及成员

    Set-ExecutionPolicy unrestricted $cred = Get-Credential  $session = New-PSSession -ConfigurationName ...

  2. .NET导出Excel之NPOI

    前段时间研究过微软的Excel导出.table输出Excel,而它们也存在一些弊端: 1.对于微软的Excel导出存在一些弊端,如:需要安装Office软件.速度问题: 2.table输出Excel在 ...

  3. Fedora 28 UEFI模式安装过程记录

    这次的折腾是个意外.不过还是要记录一下. 多次做启动盘,把U盘做坏了.将U盘用量产工具修复以后就能做启动盘了.从官网下了Fedora 28的镜像(与CentOS同属RedHat系,尽量与鸟哥一致),用 ...

  4. app开发相关

    app播放UIWebview 没有声音解决: 设置 allowsInlineMediaPlayback  = YES; mediaPlaybackRequiresUserAction = NO

  5. Scrum立会报告+燃尽图(十月二十六日总第十七次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...

  6. 强化学习之QLearning

    注:以下第一段代码是 文章 提供的代码,但是简书的代码粘贴下来不换行,所以我在这里贴了一遍.其原理在原文中也说得很明白了. 算个旅行商问题 基本介绍 戳 代码解释与来源 代码整个计算过程使用的以下公式 ...

  7. <<世界是数字的>>读书笔记

    <世界是数字的>这本书是大学职业规划老师介绍个我读的,从着本中我学到了很多. 第一章,计算机里有什么.这个问题可以从两方面来看:逻辑上或者说功能上的组成,即每一部分是什么.做什么.怎样做. ...

  8. Alpha 冲刺8

    队名:日不落战队 安琪(队长) 今天完成的任务 登录的数据post. okhttp学习第二弹. 明天的计划 okhttp学习第三弹. 个人信息界面数据get. 还剩下的任务 个人信息数据get. 遇到 ...

  9. C#高级编程 (第六版) 学习 第六章:运算符和类型强制转换

    第六章 运算符和类型强制转换 1,运算符 类别 运算符 算术运算符 + - * / % 逻辑运算符 & | ^ ~ && || ! 字符串连接运算符 + 增量和减量运算符 ++ ...

  10. [图算法] 1003. Emergency (25)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...