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 ...
随机推荐
- Spark Streaming 总结
这篇文章记录我使用 Spark Streaming 进行 ETL 处理的总结,主要包含如何编程,以及遇到的问题. 环境 我在公司使用的环境如下: Spark: 2.2.0 Kakfa: 0.10.1 ...
- (转载)Android学习之Intent使用
ndroid学习之Intent使用 1.使用显示Intent Intent intent = new Intent(FirstActivity.this,SecondActivity.class) ...
- ASP调用WebService转化成JSON数据,附json.min.asp
首先定义SOAP数据,然后创建HTTP对象,然后使用POST提交,获取状态码为200,就说明调用成功,再进行下一步操作…… <!--#Include virtual="/Include ...
- FCC编程题之中级算法篇(上)
介绍 FCC: 全称为freeCodeCamp,是一个非盈利性的.面向全世界的编程练习网站.这次的算法题来源于FCC的中级算法题. FCC中级算法篇共分为(上).(中).(下)三篇.每篇各介绍7道算法 ...
- Calling Mojo from Blink
Variants Let's assume we have a mojom file such as this: module example.mojom; interface Foo { ...
- dedecms简略标题(副标题)使用方法教程
在常见的CMS系统中,我对dedecms算是比较熟悉的,自己网站用的也是这个系统.系统功能强大使用灵活,相信这也是它受到大多数中小站长青睐的原因. 再好的系统也有照顾不周的地方,很多站长也会有自己个性 ...
- [arc076f]Exhausted? - 贪心
题意: 给你m个椅子可以坐人,初始坐标为正整数1~m,有n个人,每个人希望坐的位置$\leq L_i$或者$\geq R_i$,可以添加若干个椅子在任意的实数位置,求最少要添加多少椅子使得所有人都有位 ...
- Manacher笔记
(其实还是回文自动机好用,毛子真是牛逼) Manacher #include<iostream> #include<cstring> #include<cstdio> ...
- BZOJ 1355[Baltic2009]Radio Transmission(KMP)
题意 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. (n<=1000000) 题解 这种求最小循环节的题一般是KMP. 因为有 ...
- CAD二次开发(02)-添加对象到模型空间
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...