题目链接:http://codeforces.com/contest/954/problem/D

题意

给出n个顶点,m条边,一个起点编号s,一个终点编号t

现准备在这n个顶点中多加一条边,使得st之间距离不变

问加边的方案数是多少

思路

想了半天才出思路,头一次打比赛时通过图论的题,挺高兴

因为是加一条边,所以我们可以考虑把这个新边的两端点进行更新

现用两个dist,一个是从起点开始的单源最短路dist[0],一个是从终点开始的单源最短路dist[1]

对于一个新边的两端点ab,我们只用判断是否有dist[0][a]+dist[1][b]+e.dis>=dist[0][t] 和 dist[1][a]+dist[0][b]+e.dis>=dist[0][t],若满足就说明这个新边是可行

代码

#include <set>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> Node;
const int maxn=1000+5, maxm=1000+5, INF=0x3f3f3f3f;
struct Edge{
int from, to;
Edge(int from=0, int to=0):
from(from), to(to) {}
};
vector<Edge> edge;
vector<int> G[maxn];
set<int> emap;
int n, dist[2][maxn]; // 0 for s, 1 for t
void addEdge(int from, int to){
edge.push_back(Edge(from, to));
G[from].push_back(edge.size()-1);
G[to].push_back(edge.size()-1);
} void Dijskra(int st, int idx){
priority_queue<Node, vector<Node>, greater<Node> > que;
que.push(Node(0, st));
// for (int i=0; i<=n; i++) dist[idx][i]=INF;
dist[idx][st]=0;
while (que.size()){
Node x=que.top(); que.pop();
if (x.first!=dist[idx][x.second]) continue; int &from=x.second;
for (int i=0; i<G[from].size(); i++){
Edge &e=edge[G[from][i]]; int to=(e.from==from)?e.to:e.from;
if (dist[idx][to]<=dist[idx][from]+1) continue;
dist[idx][to]=dist[idx][from]+1;
que.push(Node(dist[idx][to], to));
}
}
} int main(void){
int s, t, m;
memset(dist, INF, sizeof(dist));
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i=0, a, b; i<m; i++){
scanf("%d%d", &a, &b);
if (a>b) swap(a, b);
addEdge(a, b);
emap.insert(a*(maxn-5)+b);
}
Dijskra(s, 0); Dijskra(t, 1); int ans=0;
for (int a=1; a<=n; a++)
for (int b=a+1; b<=n; b++){
if (emap.count(a*(maxn-5)+b)) continue;
if (dist[0][a]+dist[1][b]+1>=dist[0][t] && dist[1][a]+dist[0][b]+1>=dist[0][t])
ans++;
}
printf("%d\n", ans); return 0;
}

CodeForcesEducationalRound40-D Fight Against Traffic 最短路的更多相关文章

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

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

  2. 最短路 CF954D Fight Against Traffic

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

  3. [CodeForces954D]Fight Against Traffic(最短路)

    Description 题目链接 Solution 从起点和终点分别做一次最短路并记录结果 枚举每一条可能的边判断 Code #include <cstdio> #include < ...

  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. node,koa 图片批量添加水印,可手动配置水印位置

    公司设计在处理京东上架商品图片的时候,需要给设计好的图片添加京东的“logo”,并且logo位置得根据图片来摆放,需要通过计算得出logo位置.那样太麻烦了,于是就用node,koa写了批量给图片添加 ...

  2. java 获取线程id

    如何获取正在运行的线程的ID? 解决方法 下面的示例演示如何使用getThreadId() 方法一个正在运行线程的ID. public class Main extends Object implem ...

  3. rman备份简介

    登陆rman: [oracle@oracle ~]$ rman target / connected to target database: FSDB (DBID=1179347208) 执行全备: ...

  4. RocketMQ学习笔记(5)----RocketMQ监控平台rocketmq-console-ng的搭建

    1. 下载rocketmq-console-ng 官网地址:https://github.com/apache/rocketmq-externals 拉下来之后,使用idea打开rocketmq-co ...

  5. 将对象a的属性赋值给对象b

    BeanUtils.copyProperties(a,b); 将a的属性赋值给b(ab的共同属性)

  6. Nginx域名配置文件bak

    server { listen 80; server_name m.abd.com; rewrite ^(.*)$ https://$host$1 permanent; } server { list ...

  7. 异步调用task

    异步主要用来提升程序性能,会增加系统的开销(新建一个线程去执行异步任务). 可应用于耗时长的操作,比如:访问数据库时(应用程序和数据库不在同一台服务器上).服务之间的调用(服务会分散在不同的服务器上) ...

  8. [HDU5686]2016"百度之星" - 资格赛 Problem B

    题目大意:给你n,规定一个串中相邻的两个1可以合并为一个2(别的不行),让你求长度为n的全1串最多能变成多少种不同的串. 解题思路:我们先来找一波规律,发现n=1,2,3,4,5时答案分别为1,2,3 ...

  9. linux 安装常用库

    在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现. [root@bogon 桌面]#  yum install -y ...

  10. 【CS-4476-project 6】Deep Learning

    AlexNet / VGG-F network visualized by mNeuron. Project 6: Deep LearningIntroduction to Computer Visi ...