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

题意:

给定一张边权均为1的无向图。
问至多可以删除多少边,使得s1到t1的最短路不超过l1,s2到t2的最短路不超过l2。
 
转化成至少保留多少条边
若两条路径没有没有交集,就是dis[a1][b1]+dis[a2][b2]
如果两条路径有交集,交集一定是连续的一段,枚举交集的起点和终点即可
注意s1向t1方向可能对应着s2向t2方向,也可能对应着t2向s2方向
所以要交换s1 t1 求两遍
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 3001 int dis[N][N]; int s1,t1,s2,t2,l1,l2; int n; int tot;
int front[N],to[N*N],nxt[N*N]; int mi; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
} void bfs(int s)
{
int now,t;
static queue<int>q;
q.push(s);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=front[now];i;i=nxt[i])
{
t=to[i];
if(dis[s][t]==-)
{
dis[s][t]=dis[s][now]+;
q.push(t);
}
}
}
} void solve()
{
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
{
if(dis[s1][i]+dis[i][j]+dis[j][t1]>l1 || dis[s2][i]+dis[i][j]+dis[j][t2]>l2) continue;
mi=min(mi,dis[s1][i]+dis[j][t1]+dis[s2][i]+dis[j][t2]+dis[i][j]); }
} int main()
{
int m;
read(n); read(m);
int u,v;
for(int i=;i<=m;++i)
{
read(u); read(v);
add(u,v);
}
memset(dis,-,sizeof(dis));
for(int i=;i<=n;++i) dis[i][i]=;
for(int i=;i<=n;++i) bfs(i);
read(s1); read(t1); read(l1);
read(s2); read(t2); read(l2);
if(dis[s1][t1]>l1 || dis[s2][t2]>l2)
{
printf("-1");
return ;
}
mi=dis[s1][t1]+dis[s2][t2];
solve();
swap(s1,t1);
solve();
printf("%d",m-mi);
}
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

Codeforces 543 B. World Tour的更多相关文章

  1. Codeforces 666 B. World Tour

    http://codeforces.com/problemset/problem/666/B 题意: 给定一张边权均为1的有向图,求四个不同的点A,B,C,D,使得dis[A][B]+dis[B][C ...

  2. Codeforces#543 div2 B. Mike and Children(暴力?)

    题目链接:http://codeforces.com/problemset/problem/1121/B 题意 给n个数 最多的对数 其中每一对(i,j)的ai+aj都相等(不知道怎么解释.... 判 ...

  3. Codeforces#543 div2 A. Technogoblet of Fire(阅读理解)

    题目链接:http://codeforces.com/problemset/problem/1121/A 真·阅读理解 题意就是 有n个人 pi表示他们的强度 si表示他们来自哪个学校 现在Arkad ...

  4. 【Codeforces 1137C】Museums Tour

    Codeforces 1137 C 题意:给一个有向图,一周有\(d\)天,每一个点在每一周的某些时刻会开放,现在可以在这个图上从\(1\)号点开始随意地走,问最多能走到多少个开放的点.一个点如果重复 ...

  5. codeforces 667D D. World Tour(最短路)

    题目链接: D. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  6. CodeForces 860D Wizard's Tour

    题意 给出一张无向图,要求找出尽量多的长度为2的不同路径(边不可以重复使用,点可以重复使用) 分析 yzy:这是原题 http://www.lydsy.com/JudgeOnline/problem. ...

  7. Codeforces 543.B Destroying Roads

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

  8. codeforces 543 C Remembering Strings

    题意:若一个字符串集合里的每一个字符串都至少有一个字符满足在i位上,仅仅有它有,那么这个就是合法的,给出全部串的每一个字符修改的花费,求变成合法的最小代价. 做法:dp[i][j].前i个串的状态为j ...

  9. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

随机推荐

  1. Docker GitHub 网站中 Readme.md 以技术者的角度翻译

    Docker 是一个开源的轻量级容器项目,用于让你的应用在它上面打包.集装和运行.Docker 运行的环境既包含未知硬件也包含未知操作系统.这句话的意思是它可以运行在任何地方,小到你的笔记本大到一个大 ...

  2. GitHub 新手教程 六,Git GUI 新手教程(3),从GitHub远端同步代码库

    从GitHub把代码库下载到本地: 1,打开 GitGUI,单击我们之前克隆好的本地库: 2,按图片所示点击,同步远端代码: 3,出现如下提示后,点击“Close”: 4,上面只是把代码下载下来,还没 ...

  3. (转)Unity内建图标列表

    用法 Gizmos.DrawIcon(transform.position, "PointLight Gizmo"); UnityEditor.EditorGUIUtility.F ...

  4. Hadoop版本的选择问题

    自从2013年下半年开始,hadoop的版本开始了快速的更新换代,这和通信和互联网行业(ICT)的发展是密切相关的.随着移动网络的和宽带网络的覆盖以及数据传输速率的提升,线上的数据有了爆炸式的增长.这 ...

  5. VC++ 屏蔽掉警告

    使用VC6.0在开发程序的时候经常会遇到很多警告,很麻烦,也很耽误时间,可以使用如下方法屏蔽掉警告 在StdAfx.h 中 #define VC_EXTRALEAN 下面增加:#pragma warn ...

  6. M1阶段事后分析

    M1阶段的开发结束了,在周四的课上我们组也进行了alpha阶段的汇报.我们的努力得到了应有的回报,下面我们将针对M1阶段产生的一些问题进行分析和反思. 一.设想和目标 1.我们的app更像是一款针对北 ...

  7. 《Linux内核》课本读书笔记 第三章

  8. Beta版本冲刺(六)

    目录 组员情况 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最新成果 团队签入记 ...

  9. vs2013的安装与使用 测试

    vs2013软件我去年已经用过,可是当时只是鉴于对于c语言的编程,并没有觉得好用,况且好多的功能自并没有去深入研究,所以当时对于这个软件还是排斥的.安装的时候是别人帮我装的,所以并没有在安装的过程有问 ...

  10. 使用不同的namespace让不同的kafka/Storm连接同一个zookeeper

    背景介绍: 需要部署2个kafka独立环境,但是只有一个zookeeper集群. 需要部署2个独立的storm环境,但是只有一个zookeeper集群. ----------------------- ...