D - Destroying Roads

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/544/problem/D

Description

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 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).

Output

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

Sample Input

5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2

Sample Output

0

HINT

题意

有n个城镇,m条边权为1的双向边

让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2

题解:

跑一发最短路,然后最后留下的图肯定是出了s1-t1,s2-t2这两条路之外,其他路都被删除了

由于边权为1,那么距离就是边数

那么我们就直接枚举重叠的道路就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** vector<int> e[maxn];
int d[][];
int vis[];
int main()
{
int n=read(),m=read();
int s1,s2,t1,t2,d1,d2;
for(int i=;i<=m;i++)
{
int x=read(),y=read();
e[x].push_back(y);
e[y].push_back(x);
} scanf("%d%d%d%d%d%d",&s1,&t1,&d1,&s2,&t2,&d2);
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(i);
vis[i]=;
while(!q.empty())
{
int v=q.front();
q.pop();
for(int j=;j<e[v].size();j++)
{
int u=e[v][j];
if(vis[u])
continue;
vis[u]=;
d[i][u]=d[i][v]+;
q.push(u);
}
}
}
if(d[s1][t1]>d1||d[s2][t2]>d2)
{
puts("-1");
return ;
}
int ans=d[s1][t1]+d[s2][t2];
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(d[s1][i]+d[i][j]+d[j][t1]<=d1&&d[s2][i]+d[i][j]+d[j][t2]<=d2)
ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[j][t2]);
if(d[s1][i]+d[i][j]+d[j][t1]<=d1&&d[t2][i]+d[i][j]+d[j][s2]<=d2)
ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[t2][i]+d[j][s2]);
}
}
cout<<m-ans<<endl; }

最新文章

  1. 1JavaEE应用简介----青软S2SH(笔记)
  2. 【原创】如何在Android Studio下调试原生安卓Framework层面的源代码
  3. gbd基本使用一
  4. Web前端开发:为何选择MVVM而非MVC
  5. 【源码下载】分享一个支持自安装自卸载的Windows服务
  6. 手机h5 页面 iPhone 下 手机号码 蓝色字体 黑色字体
  7. unicode转中文
  8. 转:JavaScript函数式编程(三)
  9. WSGI详解
  10. A亚马逊WS网上系列讲座——怎么样AWS云平台上千万用户的应用建设
  11. Mars之android的Handler(2)
  12. 【翻译】EXTJS 编码风格指南与实例
  13. js 骂人不带脏字 (!(~+[]) + {})[--[~+&quot;&quot;][+[]] * [~+[]] + ~~!+[]] + ({} + [])[[~!+[]] * ~+[]] 图解
  14. #WEB安全基础 : HTML/CSS | 0x8.1CSS继承
  15. python基础 (迭代器回顾,生成器,推导式)
  16. 05 Hadoop 设置块的大小
  17. 【打印】windows打印控件,Lodop.js介绍
  18. django 神奇的双下划线,通过外键的三种查询方式
  19. 从中央仓库下载所想要的jar包
  20. Qt 4.6.2静态编译后,创建工程出现中文乱码的解决办法

热门文章

  1. thinkphp 5.0 代码执行漏洞
  2. python进阶之关键字和运算符触发魔法方法
  3. Python3 re模块正则表达式中的re.S
  4. CentOS测网速
  5. sqlalchemy更新和插入操作
  6. 【转载】selenium with PhantomJs wait till page fully loaded?
  7. git-定制属于你的log格式
  8. java中参数传递--值传递,引用传递
  9. 设计模式--工厂模式 caffe_layer注册
  10. 查看压缩包内容tar -tf