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. DOMNodeInserted,DOMNodeRemoved 和监听内容变化插件

    元素的增加 删除 及事件监听 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  2. (C# 基础) 类访问修饰符

    C# 中有5个权限修饰符,用于控制对对象的访问权限. 1. public:   访问不受限制. namespace, enum成员,interface成员 隐式的具有public 修饰符,不能在显式添 ...

  3. C++学生信息处理

    #include #include using namespace std; template int getArrayLen(T& array) //使用模板定义一个函数getArrayLe ...

  4. Azure 进阶攻略 | 文件完整性,你打算如何证明?

    假设你是一位独立软件开发者,通过自己的网站提供软件下载.网站完全托管在 Azure 中,并且软件下载也是通过 Azure Blob 存储和 Azure CDN 服务提供的. 这做法真不错,不需要自己管 ...

  5. python3线程介绍01(如何启动和调用线程)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...

  6. Linux目录与文件的权限意义

    ls -l和ls -al的区别:第一个不会显示隐藏文件,第二个会显示隐藏文件(以点(.)开头的文件) 一.权限对文件(r.w.x主要针对文件的内容而言)的重要性 r:可读取文件内容 w:可以编辑.新增 ...

  7. U深度U盘启动盘制作工具怎么用?U深度U盘启动盘制作工具使用教学

    U深度u盘启动盘制作工具是一款强大的启动盘制作软件,对于新手用户来说,由于软件专业度很高,想一下就上手是比较困难的.所以这里给大家分享一篇U深度u盘启动盘制作工具的使用教程. 使用教程: 第一步:安装 ...

  8. centos 7中磁盘挂载重启后挂载失效

     在centos 7磁盘挂载成功后,关机重启,挂载磁盘失效,需要重新挂载,不用重新挂载的开机挂载方法如下: 1.先检验要挂载的磁盘是否已被挂载,有的话先卸除 2.修改 /etc/fstab 文件 ,最 ...

  9. [转载]弹出一个不带地址栏、工具栏的IE非模态窗口

    标签:ie /非模态窗口 window.open(url,'_blank','menubar=no,fullscreen=1,toolbar=no,resizable=no,location=no,s ...

  10. v-for的深层用法

    为了提升循环的性能,我们会给循环加上一个唯一的key值,这个key值一定是唯一的 <div id='root'> <div v-for='(item,index) of list' ...