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. json-server+mockjs 模拟REST接口

    前言: 项目开发中,影响项目进程的常常是由于在前后端数据交互的开发流程中停滞,前端完成静态页面的开发后,后端迟迟未给到接口.而现在,我们就可以通过根据后端接口字段,建立一个REST风格的API接口,进 ...

  2. web项目_学生证管理系统

    项目简述: 基于java web实现学生卡管理系统,用户包括学生和学生处,分别实现以下功能. 学生: 1.对本人登录密码的修改 2.申请补办学生卡 3.查看学生卡补办状态 学生处: 1.对学生信息的管 ...

  3. (功能篇)回顾Bug管理系统Mantis优化改造经历

    共分为两篇,功能篇和技术篇. 时间大约是2016年冬天. 考虑搭一个用于Bug管理和追踪的系统. 综合比较下,选择了小巧的开源工具,Mantis. 在源码基础上,做代码修改,完成了定制版的优化改造. ...

  4. webpack 打包

    React自发布以来吸引了越来越多的开发者,React开发和模块管理的主流工具webpack也被大家所熟知.那么webpack有哪些优势,可以成为最主流的React开发工具呢? webpack是什么 ...

  5. wordpress学习五: 通过wordpress_xmlrpc的python包远程操作wordpress

    wordpress提供了丰富的xmlrpc接口api来供我们远程操控wp的内容.伟大的开源社区有人就将这些api做了一下封装,提供了一个功能比较完整的python库,库的使用文档地址http://py ...

  6. 基于 Java Web 的毕业设计选题管理平台--测试报告与用户手册

    一.测试报告 1.兼容性测试 功能 描述 效果 Chrome浏览器 FireFox浏览器 IE浏览器 war 端浏览器 管理员登录 管理员用户登录功能 弹出“登录成功”对话框,进入到毕业设计选题管理平 ...

  7. yum 安装mongodb mysql

    // 云环境下更新包 (center os)yum update (多更有益) 修改yum包管理配置:vi /etc/yum.repos.d/mongodb-org-3.4.repo // 会自动新建 ...

  8. 使用phantomjs进行无界面UI自动化测试

    PhantomJS(http://phantomjs.org/) 是一个基于WebKit的服务器端JavaScript API.它全面支持web而不需浏览器支持,其快速.原生支持各种Web标准:DOM ...

  9. bower安装和使用

    bower的安装 1,首先在我的系统 安装 nodejs.因为我的系统是windows,还需要安装msysgit,注意图二中的选项   msysgit   Git setup 2,之后就可以用npm包 ...

  10. HGOI NOIP模拟4 题解

    NOIP国庆模拟赛Day5 题解 T1 马里奥 题目描述 马里奥将要参加 NOIP 了,他现在在一片大陆上,这个大陆上有着许多浮空岛,并且其中一座浮空岛上有一个传送门,马里奥想要到达传送门从而前往 N ...