链接:http://codeforces.com/problemset/problem/543/B

B. Destroying Roads

time limit per test:2 seconds

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 alongthe roads.

You want to destroy the largest possible number of roads in the country

so that the remainingroads would allow you to get from city s1 to city t1

in at most l1 hours and get from city s2 tocity t2 in at most l2 hours.

Determine what maximum number of roads you need to destroy in order

to meet the conditionof 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 cityto 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

题意:

给出一张n个点,m条边的无向图,问最多能删除几条边,

同时保证给定点之间的距离<=某一值。

or 给定n个点m条边的无向图(边权全为1),让你去掉最多的边使得d(s1, t1) <= l1

&& d(s2, t2) <= l2,若不能满足输出-1,反之输出可以去掉的最多边数。

Examples

思路:

例如给出的两条限制路径为a->...->b,c->...->d。如果这两条路径不相交的话,

显然去掉的最多边数=m-dist【a】【b】-dist【c】【d】。dist可以用spfa求一下最短路,

边权均为1。别的肯定会都超时,spfa有不超时的可能。

然后当两条路径之间有公共路径时,我们将公共路径作为中间点更新最小保留边数,

又因为n只有10^3,所以可以两重循环枚举所有边,不是最短路中公共边的边产生的

答案肯定要比最优解大的,所以只是多几个无用的冗余判断,不影响最终结果。其次

注意的是四个点之间的不同的位置关系有两种,分别更新一下答案即可。

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

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define N 3005
#define inf 0x3f3f3f3f
struct Edge
{int to,next,w;
}edge[N*N];
int cnt,n,head[N],low[N][N],w[N];
bool vis[N];
inline void add(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;
}
void spfa()
{int i,j;for(j=1; j<=n; ++j){memset(vis,0,sizeof(vis));low[j][j]=0;vis[j]=1;queue<int> q;q.push(j);while(!q.empty()){int u=q.front();q.pop();vis[u]=0;for(i=head[u]; ~i; i=edge[i].next){int v=edge[i].to;if(low[j][v]>low[j][u]+edge[i].w){low[j][v]=low[j][u]+edge[i].w;if(!vis[v]){vis[v]=1;q.push(v);}}}}}
}
int main()
{int m,i,j,x,y,s1,t1,s2,t2,l1,l2;cin>>n>>m;memset(head,-1,sizeof(head));memset(low,inf,sizeof(low));for(i=1;i<=m;++i){scanf("%d%d",&x,&y);add(x,y,1);add(y,x,1);}spfa();scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2);if(low[s1][t1]>l1||low[s2][t2]>l2) puts("-1");else{int ans=low[s1][t1]+low[s2][t2];for(i=1;i<=n;++i)for(j=1;j<=n;++j){if(low[s1][i]+low[i][j]+low[j][t1]<=l1&&low[s2][i]+low[i][j]+low[j][t2]<=l2)ans=min(ans,low[s1][i]+low[i][j]+low[j][t1]+low[s2][i]+low[j][t2]);if(low[s1][i]+low[i][j]+low[j][t1]<=l1&&low[t2][i]+low[i][j]+low[j][s2]<=l2)ans=min(ans,low[s1][i]+low[j][t1]+low[t2][i]+low[i][j]+low[j][s2]);}printf("%d\n",m-ans);}return 0;
}

The end;

Codeforces 544D Destroying Roads相关推荐

  1. 训练3.21(CF 543B Destroying Roads)

    训练3.21(CF543B Destroying Roads) 题目:http://codeforces.com/problemset/problem/543/B 题意:有n点m边(n<3000 ...

  2. Codeforces 773D Perishable Roads 最短路 (看题解)

    Perishable Roads 智商题, 不会啊.. 贴个官方题解 https://codeforces.com/blog/entry/51883 #include<bits/stdc++.h ...

  3. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Codeforces 835 F Roads in the Kingdom(树形dp)

    F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...

  5. Codeforces C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. CodeForces - 722C Destroying Array(倒着并查集+离线处理)

    题目链接:点击查看 题目大意:给出一个数列a,现在给出操作b,每次操作都会删除掉数列a中指定位置的数,问每次删除后,最大连续字段和是多少 题目分析:一开始看到最大连续字段和,以为是要用dp,又看了一下 ...

  7. CodeForces 722C Destroying Array

    并查集,离线操作. 将操作倒着进行,一开始所有数字都没有加入到数组中,然后倒着一个一个加入,更新最大值. #pragma comment(linker, "/STACK:1024000000 ...

  8. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

  9. codeforces 722C Destroying Array

    题意:给出长度为n的序列,每次去掉一个数,使得序列分成几块,问当前权值最大块的权值. 思路:提供两种,第一种用multiset和前缀和直接暴力模拟. 第二种用并差集,假设坐标i为一个去掉的数,那么f[ ...

最新文章

  1. 实验一 编程 Hello World
  2. C#线程系列(3):线程池和文件下载服务器
  3. 如何找同服务器下网站,利用bing查询同一服务器IP下的所有网站
  4. 传智书城首页设计代码_(自适应手机版)响应式创意餐饮酒店装饰设计类网站织梦模板 html5蓝色餐饮酒店设计网站源码下载...
  5. Django单元测试
  6. mysql -b -e_为什么 MongoDB 索引选择B-树,而 Mysql 索引选择B+树(精干总结)
  7. 重写equals()和hashcode()方法详解
  8. ubuntu18.04安装ros-melodic
  9. Flash Player10一个非常牛的功能SaveBitmap
  10. Q116:PBRT-V3场景描述文件.pbrt格式解析
  11. Ps,Lr,Pr,Ae,Au,C4D,达芬奇调色
  12. matlab中blur函数_matlab-----均值滤波函数的实现
  13. VB操作EXCEL表的常用方法
  14. BootstrapTable的列排序怎么搞
  15. 基于商品属性的相似度模型
  16. 常用的配电箱有哪几种?
  17. 从vivo Photo Lab“影像实验室”透视门店新价值
  18. gitlab cicd配置
  19. js随机生成验证码(数字+字母)
  20. 朱清时看何谓大学生之“大”

热门文章

  1. 用计算机术语赞美老师,歌颂老师的排比句
  2. RabbitMQ Centos7 安装以及使用
  3. JPG图片的高宽解析.
  4. 2017第8届中国中西部(昆明)医疗器械展览会会刊(参展商名录)
  5. c++操作word接口
  6. 编写一个程序,提示用户输入身高单位(英寸,米,厘米)包括姓名使用float类型
  7. android websocket client 如何调用autobahn.jar
  8. dos下安装操作系统!
  9. WordPress自定义简码制作写法
  10. 大话西游-奇葩的周导