CodeForcesEducationalRound40-D Fight Against Traffic 最短路
题目链接: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 最短路的更多相关文章
- 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 ...
- [CodeForces954D]Fight Against Traffic(最短路)
Description 题目链接 Solution 从起点和终点分别做一次最短路并记录结果 枚举每一条可能的边判断 Code #include <cstdio> #include < ...
- 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 ...
随机推荐
- [jzoj NOIP2018模拟11.02]
嗯T1忘记取模了,100到20 嗯T2忘记了那啥定理,暴力也写炸了,这题我认 嗯T3线段树合并分裂没有写炸,考场上就知道妥妥的70分.但是,分数出的时候听到有人说暴力也是70分,我???脸黑,枉我敲了 ...
- jQuery学习(七)——使用JQ完成下拉列表左右选择
1.需求:实现以下功能 2.步骤分析: 第一步:确定事件(鼠标单击事件click) 第二步:获取左侧下拉列表被选中的option($(“#left option:selected”)) [假设左侧se ...
- mybatis批量插入、批量更新和批量删除
转载 https://www.jianshu.com/p/041bec8ae6d3
- 设置IE浏览器文档模式版本
浏览器版本问题是所有开发web的程序员共同的诟病,尤其是IE浏览器,笔者上次修改公司的一个项目中出现项目是在IE9没有出来之前,而现在修改时,则发现页面上出现的表格的错乱(因为表格是通过js动态生成的 ...
- HDU 1856 More is better【并查集】
解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值 More is better Time Limit: 5000/1000 MS (J ...
- shell-1.shell概述、2.shell脚本执行方式
目录
- Dropout 下(关于《Dropout: A Simple way to prevent neural networks from overfitting》)
先上菜单: 摘要: Deep neural nets with a large number of parameters are very powerful machine learning syst ...
- tinymce原装插件源码分析(五)-searchreplace
searchreplace 功能:查找和替换 代码注释见: https://www.xunhanliu.top/static/js/tinymce/plugins/searchreplace/plug ...
- rdesktop 脚本
[root@Eren liwm]# cat rdesktop.sh #!/bin/bash -rdesktop -u user 192.168.122.10 -r sound:local -g 10 ...
- js 函数基础(方便复习使用)
// 函数声明: function bbq(){ // ..... } // 函数表达式: // 1.命名函数表达式 var test = function abc(){ document.write ...