最短路 CF954D Fight Against Traffic
CF954D Fight Against Traffic
题意描述:
给你一张无向图,一共有n个点(2 <= n <= 1000),由m条边连接起来(1 <= m <= 10000),现在要在任意一对没有连边的点之间连上一条边,并且保证s到t之间的最短路径长度不变(最短路径长度表示s到t最少经过的边的数量)(1 <= s,t <= n , s≠t),请你求出一共有多少条这样的边。
被水题成功报复。。。
code:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
const int wx=2017;
int head[wx],dis1[wx],dis2[wx];
int vis[wx],fl[wx][wx];
int num,n,m,s,t,ans;
struct e{
int nxt,to,dis;
}edge[wx*2];
void add(int from,int to,int dis){
edge[++num].nxt=head[from];
edge[num].to=to;
edge[num].dis=dis;
head[from]=num;
}
struct node{
int u,d;
friend bool operator < (const node & a,const node & b){
return a.d>b.d;
}
};
priority_queue<node > q;
void Dij(int S,int dis[]){
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f,vis[i]=0;
dis[S]=0; q.push((node){S,0});
while(q.size()){
int u=q.top().u; q.pop();
if(vis[u])continue; vis[u]=1;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].dis){
dis[v]=dis[u]+edge[i].dis;
q.push((node){v,dis[v]});
}
}
}
}
int main(){
n=read(); m=read();
s=read(); t=read();
for(int i=1;i<=m;i++){
int x,y;
x=read(); y=read();
add(x,y,1); add(y,x,1);
fl[x][y]=1; fl[y][x]=1;
}
Dij(s,dis1); Dij(t,dis2);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(fl[i][j])continue;
int tmp1=dis1[i]+dis2[j]+1;
int tmp2=dis1[j]+dis2[i]+1;
if(min(tmp1,tmp2)>=dis1[t])ans++;
}
}
printf("%d\n",ans);
return 0;
}
最短路 CF954D Fight Against Traffic的更多相关文章
- Codeforces 954D Fight Against Traffic(BFS 最短路)
题目链接:Fight Against Traffic 题意:有n个点个m条双向边,现在给出两个点S和T并要增加一条边,问增加一条边且S和T之间距离不变短的情况有几种? 题解:首先dfs求一下S到其他点 ...
- [CodeForces954D]Fight Against Traffic(最短路)
Description 题目链接 Solution 从起点和终点分别做一次最短路并记录结果 枚举每一条可能的边判断 Code #include <cstdio> #include < ...
- CodeForcesEducationalRound40-D Fight Against Traffic 最短路
题目链接:http://codeforces.com/contest/954/problem/D 题意 给出n个顶点,m条边,一个起点编号s,一个终点编号t 现准备在这n个顶点中多加一条边,使得st之 ...
- 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 ...
随机推荐
- Linux hostname主机名配置文件与文件 /etc/hosts解析(copy来的,原作者看到了别打我)
1.关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) hosts文件是Linux系统中一个负 ...
- [C++] NULL VS nullptr
NULL VS nullptr
- LinuxSystemProgramming-vi
Basic VI
- 爬虫 之 scrapy-redis组件
scrapy-redis组件 scrapy-redis是一个基于redis的scrapy组件,通过它可以快速实现简单分布式爬虫程序,该组件本质上提供了三大功能: scheduler - 调度器 dup ...
- LWIP带UCOS操作系统移植
LWIP支持RAW.NETCONN.SOCKET这三种编程接口,后两者必须有操作系统来支持的:LWIP带操作系统的移植很重要!!
- 使用Selenium&PhantomJS的方式爬取代理
前面已经爬取了代理,今天我们使用Selenium&PhantomJS的方式爬取快代理 :快代理 - 高速http代理ip每天更新. 首先分析一下快代理,如下 使用谷歌浏览器,检查,发现每个代理 ...
- sql insert、update、delete完以后返回主键ID
以前只用过在insert完以后利用select @@IDENTITY返回主键ID,最近在做微信公众平台,遇到一个需求是在帮绑定万微信openid后自动完成登陆,这就需要update以后返回主键ID,查 ...
- C++学习--入口函数
在学习第一个C++程序的时候发现控制台程序的入口函数是int _tmain而不是main,查了资料才发现_tmain()是为了支持unicode所使用的main一个别名,宏定义在<stdafx. ...
- 【前端布局】px与rpx的简述
本文只以移动设备为例说明. 注意:设计师以iphone6为标准出设计稿的话,1rpx=0.5px=1物理像素.Photoshop里面量出来的尺寸为物理像素点.所以可以直接使用标注尺寸数据. [像素Pi ...
- Hadoop的Windows伪分布式学习
解压hadoop-2.7.2.zip,不是tar.gz,前者是Windows所用的 解压到路径,设置环境变量 HADOOP_HOME=E:\hadoop-2.7.2\ HADOOP_USER_HOME ...