[CodeForces954D]Fight Against Traffic(最短路)
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(最短路)的更多相关文章
- CodeForcesEducationalRound40-D Fight Against Traffic 最短路
题目链接:http://codeforces.com/contest/954/problem/D 题意 给出n个顶点,m条边,一个起点编号s,一个终点编号t 现准备在这n个顶点中多加一条边,使得st之 ...
- 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 ...
- Fight Against Traffic -简单dijkstra算法使用
题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...
- Codeforces 954 D Fight Against Traffic
Discription Little town Nsk consists of n junctions connected by m bidirectional roads. Each road co ...
- Educational Codeforces Round 40 (Rated for Div. 2) Solution
从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...
- Educational Codeforces Round 40 A B C D E G
A. Diagonal Walking 题意 将一个序列中所有的\('RU'\)或者\('UR'\)替换成\('D'\),问最终得到的序列最短长度为多少. 思路 贪心 Code #include &l ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- 快速切题 sgu103. Traffic Lights 最短路 难度:1
103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...
随机推荐
- Android Studio 小技巧(2):AS中Button文字默认大写的问题
问题类型 设置Layout中添加一个Button <Button android:id="@+id/bt_showerror" android:layout_width=&q ...
- Python开发环境Wing IDE如何使用GTK和PyGObject
Wing IDE是一个集成开发环境,可用于编辑.测试和调试使用PyGObject为GTK编写的Python代码.Wing IDE提供自动完成.调用提示.一个强大的调试器,以及许多其他功能,可帮助用户编 ...
- typedef struct 与 struct
学c++之前最好先学c.特别要说的是,一些虽然冠名为c++的项目的文件中却大部分都是c的代码. 比如我们这个例子: 在c语言中,定义一个结构体和其实适合c++中有区别的.比如我们有如下的代码: str ...
- Unity Shader 阴影
最近在看Unity shader开发实战详解,刚开始看阴影部分,稍微有了点思路.在这里写点笔记,算是小结吧. .阴影实现方法 一种是通过模拟光照的原理,用向量的方法 找到被光线照射的点 映射到平面的位 ...
- Apache服务器开启gzip压缩的支持
为什么要在服务器上开启压缩?其实,服务器上开启压缩,对整个网站的就是在服务器上把网页的内容压缩后传给客户端,客户端解压后再显示网页的内容.实际就是增加了服务器端和客户端的工作量,减少了网络传输的数据量 ...
- IOS tableView的性能优化(缓存池)
使用缓存池(标识类型) 1.通过 一个 标识 去 缓存池 中寻找可循环得用的cell 2.如果缓存池找不到可循环得用的cell:创建一个新的cell(给cell贴个标识) 3.给cell设置新的数据 ...
- POJ-2503 Babelfish---map或者hash
题目链接: https://vjudge.net/problem/POJ-2503 题目大意: 就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10) 解题思路: ma ...
- poj 3485 区间选点
题目链接:http://poj.org/problem?id=3485 题意:X轴上公路从0到L,X轴上下有一些点给出坐标代表村庄,问在公路上最少建几个出口才能使每个村庄到出口的距离不超过D. 以村庄 ...
- 在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'> Time Elapsed: 0:00:21.3163 ...
- Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
https://zhuanlan.zhihu.com/p/23006190?refer=xiaoleimlnote http://blog.csdn.net/bea_tree/article/deta ...