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. 重置Oracle配置

    经常被ORACLE坑,作为一个只需要开发时候连连ORACLE的程序员,在经历了一次又一次的折腾之后,决定还是把这些琐碎的事情写下来. 经常在虚拟机中使用ORACLE,ORACLE的网络配置有一些变化就 ...

  2. 软件测试--w模型

    W模型 优点:开发把随着整个开发周期,需求.和设计同样要测试,更早的介入测试,可以发现初期的缺陷,修复成本低:分阶段工作方便项目整体管理: 缺点:开发和测试依然是线性关系,需求的变更和调整,依然不方便 ...

  3. Theory And Practice

    实践出真知,建议不要一味地看Backbone源码和网上解析,自己动手实践吧少年们! 我是一个简单的简直无可救药的小栗子~ ——Silun Wang 我的几个小问题: 1. Rocket介绍没有Todo ...

  4. Notes of Daily Scrum Meeting(12.19)

    今天工作进展的速度别昨天稍有提高,希望大家再接再厉!加油! 团队任务总结如下: 团队成员 今日团队工作 陈少杰 重新尝试使用get等方法进行网络连接的调试 王迪 调试搜索功能中测出的问题 金鑫 测试已 ...

  5. C语言版本:单链表的实现

    slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #inc ...

  6. 小学四则运算APP 第一个冲刺阶段 第一天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第一次冲刺阶段时间:11.17~11.27 思考:初步了解小学四则运算数是在100以内的加减乘除,首先先从简单的地方入手,把最基础的算法功 ...

  7. 03-java学习-基本数据类型-运算符-键盘接收用户输入

    java的八大基本数据类型: 类型转换的基本原则: java整数的默认类型是int,小数的默认类型是double 运算符: 算术运算符.连接.赋值.关系.逻辑.三目运算符等…… 键盘接收用户输入: j ...

  8. redis集群的测试

    原文:http://blog.sina.com.cn/s/blog_53b45c4d0102wg10.html 1.测试存取值 客户端连接集群redis-cli需要带上 -c ,redis-cli - ...

  9. Win2008r2 设置 多用户同时远程

    Study From http://blog.sina.com.cn/s/blog_7ebe66420101tfln.html 1. 启动远程桌面,关闭防火墙 略过不提 2. 添加远程服务角色, 打开 ...

  10. Js单元测试工具 以及 粗浅的对我的快乐运算进行测试

    1. Karma的介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的 ...