Description

题目链接

Solution

从起点和终点分别做一次最短路并记录结果

枚举每一条可能的边判断

Code

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define N 1010
using namespace std; struct info{int to,nex;}e[N*2];
int n,m,s,t,tot,head[N],dis[N],ddis[N],Ans;
bool g[N][N]; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} inline void Link(int u,int v){
e[++tot].to=v;e[tot].nex=head[u];head[u]=tot;
} bool vis[N];
void bfs(int d[],int s){
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s);
d[s]=0,vis[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(vis[v]) continue;
d[v]=d[u]+1;
q.push(v);
vis[v]=1;
}
}
} int main(){
n=read(),m=read(),s=read(),t=read();
while(m--){int u=read(),v=read();Link(u,v);Link(v,u);g[u][v]=g[v][u]=1;}
bfs(dis,s);
bfs(ddis,t);
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
if(!g[i][j]&&dis[i]+ddis[j]+1>=dis[t]&&dis[j]+ddis[i]+1>=dis[t])
Ans++;
printf("%d\n",Ans);
return 0;
}

[CodeForces954D]Fight Against Traffic(最短路)的更多相关文章

  1. CodeForcesEducationalRound40-D Fight Against Traffic 最短路

    题目链接:http://codeforces.com/contest/954/problem/D 题意 给出n个顶点,m条边,一个起点编号s,一个终点编号t 现准备在这n个顶点中多加一条边,使得st之 ...

  2. Codeforces 954D Fight Against Traffic(BFS 最短路)

    题目链接:Fight Against Traffic 题意:有n个点个m条双向边,现在给出两个点S和T并要增加一条边,问增加一条边且S和T之间距离不变短的情况有几种? 题解:首先dfs求一下S到其他点 ...

  3. 最短路 CF954D Fight Against Traffic

    CF954D Fight Against Traffic 题意描述: 给你一张无向图,一共有n个点(2 <= n <= 1000),由m条边连接起来(1 <= m <= 100 ...

  4. Fight Against Traffic -简单dijkstra算法使用

    题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...

  5. Codeforces 954 D Fight Against Traffic

    Discription Little town Nsk consists of n junctions connected by m bidirectional roads. Each road co ...

  6. Educational Codeforces Round 40 (Rated for Div. 2) Solution

    从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...

  7. Educational Codeforces Round 40 A B C D E G

    A. Diagonal Walking 题意 将一个序列中所有的\('RU'\)或者\('UR'\)替换成\('D'\),问最终得到的序列最短长度为多少. 思路 贪心 Code #include &l ...

  8. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  9. 快速切题 sgu103. Traffic Lights 最短路 难度:1

    103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...

随机推荐

  1. Android 关于apk 打包后的地图定位和导航失败的问题

    项目中,使用了高德地图定位,调试的debug包定位完全没有问题,但是签名打包后,却始终无法定位,发现是测试环境下的SHA1码和签名发布版的SHA1码是不同的. 所以我们需要获取发布版的SHA1码: 方 ...

  2. *1 Two Sum two pointers(hashmap one scan)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. framework7 v2.x轮播图写法:

    <div class="swiper-container swiper-init travel-index-swiper"> <div class="s ...

  4. python 爬糗事百科

    糗事百科网站段子爬取,糗事百科是我见过的最简单的网站了!!! #-*-coding:utf8-*- import requests import re import sys reload(sys) s ...

  5. HDU(1754),线段树,单点替换,区间最值

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include < ...

  6. (转)Fidder详解之get和post请求

    https://www.cnblogs.com/langhuagungun/p/7737204.html 前言 本文会对Fidder这款工具的一些重要功 能,进行详细讲解,带大家进入Fidder的世界 ...

  7. EF 连接 mysq l数据库 code first模式 的实践

    准备工作: 1.下载vs2015 2.下载mysql2017 3.安装 开始: 1.创建 控制台文件 2.添加引用 Mysql.Data , Mysql.Data.Entity.EF6,Mysql.w ...

  8. Java连接mysql中遇到的一些问题及解决方法

    1.Java使用mysql-jdbc连接MySQL出现如下警告: Establishing SSL connection without server's identityverification i ...

  9. Ubuntu 10.04上安装MongoDB

    MongoDB是一个可扩展.高性能的下一代数据库.MongoDB中的数据以文档形式存储,这样就能在单个数据对象中表示复杂的关系.文档可能由 以下几 部分组成:独立的基本类型属性.“内嵌文档”或文档数组 ...

  10. django.template.exceptions.TemplateSyntaxError: 'article_tags' is not a registered tag library.

    django.template.exceptions.TemplateSyntaxError: 'article_tags' is not a registered tag library. Must ...