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 ...
随机推荐
- 64位系统上32位进程拷贝文件到System32目录时的重定向
64位系统上,32位进程拷贝文件到"System32"目录时,会被文件系统重定向到"SysWOW64"目录 要禁用这种重定向,需要用到下面2个API: Wow6 ...
- SVN与TFS自动同步脚本(很实用)
一直都在园子里看文章,因为各种原因懒得写文章.最近稍得空闲,把这几天的工作成果分享一下. 因为工作需要,开发人员使用Qt进行系统移动端的开发,Qt的版本控制却不提供连接TFS的设置,只有使用svn.没 ...
- iOS猜拳游戏源码
利用核心动画和Quartz2D做的一个小游戏.逻辑十分简单. 源码下载:http://code.662p.com/<ignore_js_op> 详细说明:http://ios.662p.c ...
- 数据库管理系统X
大部分DBMS提供数据定义语言DDL(Data Definition Language)和数据操作语言DML(Data Manipulation Language),供用户定义数据库的模式结构与权限约 ...
- 7-Java-C(四平方和)
题目描述: 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和. 如果把0包括进去,就正好可以表示为4个数的平方和. 比如: 5 = 0^2 + 0^2 + 1^2 + ...
- dialog - 从 shell 显示对话框
总览 (SYNOPSIS) dialog --clear dialog --create-rc file dialog --print-maxsize dialog common-options bo ...
- navicat 链接数据库查看的工具 可以同时查看各种数据库 MySql SqlServer
navicat 链接数据库查看的工具 Navicat_Premium_10.0.11.0_XiaZaiBa
- #PHP#微信支付 第二篇 JSAPI 调用统一下单接口获取预支付交易数据
上一篇讲到成功获取 openid,本篇要调用微信统一接口创建预支付交易单,并获取到相关数据,以便(后边)在微信内调起H5支付 第三步,调用微信统一下单接口创建预支付交易单 微信统一下单API是微信支付 ...
- ubuntu卡机
卡机了用ctrl+alt+t打开终端然后top看后台程序 最后kill -9 + PID就能把最影响问题的程序杀掉 我之前就杀了一个占100%cpu的程序
- EBS ORACLE工单齐套率的计算程序
PROCEDURE Get_wip_accept_item_date(p_use_id in number, p_org_id IN NUMBER, p_start_date IN DATE, p_e ...