Codeforces 954 D Fight Against Traffic
Discription
Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them.
In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction s to work located near junction t. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease.
You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won't decrease.
Input
The firt line of the input contains integers n, m, s and t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, 1 ≤ s, t ≤ n, s ≠ t) — the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The i-th of the following m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), meaning that this road connects junctions ui and vi directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.
Output
Print one integer — the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions s and t.
Example
5 4 1 5
1 2
2 3
3 4
4 5
0
5 4 3 5
1 2
2 3
3 4
4 5
5
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
3 两遍dfs之后暴力判断即可。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1005;
bool a[maxn][maxn],v[maxn];
int n,m,S,T,d[maxn],g[maxn],ans;
int to[maxn*2],ne[maxn*2],hd[maxn]; inline void BFS(){
queue<int> q; int x;
q.push(S),v[S]=1;
while(!q.empty()){
x=q.front(),q.pop();
for(int i=hd[x];i;i=ne[i]) if(!v[to[i]]){
v[to[i]]=1,d[to[i]]=d[x]+1;
q.push(to[i]);
}
} memset(v,0,sizeof(v));
q.push(T),v[T]=1;
while(!q.empty()){
x=q.front(),q.pop();
for(int i=hd[x];i;i=ne[i]) if(!v[to[i]]){
v[to[i]]=1,g[to[i]]=g[x]+1;
q.push(to[i]);
}
}
} int main(){
scanf("%d%d%d%d",&n,&m,&S,&T);
int uu,vv;
for(int i=1;i<=m;i++){
scanf("%d%d",&uu,&vv),a[uu][vv]=a[vv][uu]=1;
to[i]=vv,ne[i]=hd[uu],hd[uu]=i;
to[i+m]=uu,ne[i+m]=hd[vv],hd[vv]=i+m;
} BFS(); for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++) if(!a[i][j])
if(d[i]+g[j]>=d[T]-1&&d[j]+g[i]>=d[T]-1) ans++; printf("%d\n",ans);
return 0;
}
Codeforces 954 D Fight Against Traffic的更多相关文章
- Codeforces 954D Fight Against Traffic(BFS 最短路)
题目链接:Fight Against Traffic 题意:有n个点个m条双向边,现在给出两个点S和T并要增加一条边,问增加一条边且S和T之间距离不变短的情况有几种? 题解:首先dfs求一下S到其他点 ...
- 最短路 CF954D Fight Against Traffic
CF954D Fight Against Traffic 题意描述: 给你一张无向图,一共有n个点(2 <= n <= 1000),由m条边连接起来(1 <= m <= 100 ...
- Codeforces 954 E. Water Taps
http://codeforces.com/problemset/problem/954/E 式子变成Σ xi*(ti-T)=0 sum0表示>=T的ai*ti之和 sum1表示<T的ai ...
- Codeforces 954 G. Castle Defense
http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...
- Fight Against Traffic -简单dijkstra算法使用
题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...
- CodeForcesEducationalRound40-D Fight Against Traffic 最短路
题目链接:http://codeforces.com/contest/954/problem/D 题意 给出n个顶点,m条边,一个起点编号s,一个终点编号t 现准备在这n个顶点中多加一条边,使得st之 ...
- codeforces 487A A. Fight the Monster(二分)
题目链接: A. Fight the Monster time limit per test 1 second memory limit per test 256 megabytes input st ...
- [CodeForces954D]Fight Against Traffic(最短路)
Description 题目链接 Solution 从起点和终点分别做一次最短路并记录结果 枚举每一条可能的边判断 Code #include <cstdio> #include < ...
- Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check
A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...
随机推荐
- 四次元新浪微博客户端Android源码
四次元新浪微博客户端Android源码 源码下载:http://code.662p.com/list/11_1.html [/td][td] [/td][td] [/td][td] 详细说明:http ...
- 最新精品 强势来袭 XP,32/64位Win7,32/64位Win8,32/64位Win10系统【国庆版版】
本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...
- upupw nginx服务器 rewrite设置
最近开始尝试使用upupw的Nginx套件做开发,感觉还挺不错的,也遇到了一些问题,决定在这里记录一下,同时也希望可以帮助到一些人. 用习惯了Apache,改用Nginx之后会有些不适应,但是咬咬牙就 ...
- 腾讯AI开放平台的接口调用指南
最近无意发现腾讯AI开放平台上提供了大量好玩的人工智能云服务,而且是完全免费的.只需要用QQ号登录即可.这么好的东西,作为一个程序员,当然要试试了! 从上图可以看出腾讯AI开放平台提供的人工智能服务主 ...
- MPP(大规模并行处理)简介
1. 什么是MPP? MPP (Massively Parallel Processing),即大规模并行处理,在数据库非共享集群中,每个节点都有独立的磁盘存储系统和内存系统,业务数据根据数据库模型和 ...
- Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras 论文摘要
1. Abstract 通过一种Unified Omnidirectional Model作为投影方程. 这种方式可以使用图像的所有内容包括有强畸变的区域,而现存的视觉里程计方案只能修正或者切掉来使用 ...
- [BZOJ2120]:数颜色(分块?)
题目传送门 我感觉这种题没必要扯淡题目大意了,没啥用. 暴力过掉,擦了个边. 主要是讲一下这道题我用到的卡常. 首先,0,1标记我用的位运算,位运算符跑的要比正常的+,-,×,÷,true,false ...
- mysql查询速度慢的原因[整理版]
在以前的博客中陆续记录了有关查询效率方面的文章.今天在整理一下,写上自己的一些心得记录如下:常见查询慢的原因常见的话会有如下几种:1.没有索引或没有用到索引.PS:索引用来快速地寻找那些具有特定值的记 ...
- 模拟、字符串--P1042 乒乓球 题解
P1042 乒乓球 字符串string的基本使用 #include <iostream> #include <algorithm> #include <map> # ...
- Hadoop集群安装指南(CHD5.9.1)(分布式+图文详解)
centos7.1,CDH5.9.1,3台机器,终极指导安装 下载链接如下: 安装文件下载链接如下: 链接:https://pan.baidu.com/s/1RQYNiWn9a-T8GXcCsoDBs ...