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 ...
随机推荐
- JS数组去重 包含去除多个 NaN
Array.prototype.uniq = function () { var arr = []; var flag = true; this.forEach(function(item) { ...
- FPGA初学之前后仿真分析
最近在学习FPGA,感觉语言的学习到时很容易,但是由于缺乏电路图的硬件知识,所以看起来比较难懂,下面是对FPGA中仿真的一点理解,以后需要学习的地方还有很多啊. 一.使用ISE环境进行FPGA系统设计 ...
- Java文件(io)编程——文件字符流的使用
案例1: 读取一个文件并写入到另一个文件中,char[] 来中转. 首先要在E盘下创建一个文本文档,命名为test.txt,输入一些字符串. public class Demo_5 { public ...
- nginx的location 匹配的规则问题
正则解释: ~ #匹配一个正则匹配,区分大小写~* #匹配一个正则,不区分大小写^~ #普通字符匹配,如果该选择匹配不匹配别的选项,一般用来匹配目录= #精确匹配 匹配案例:location = / ...
- NetworkX-simple graph
import networkx as nx import matplotlib.pyplot import scipy.io as sio import numpy as np load_path=' ...
- WPF 列表自动换行
原文:WPF 列表自动换行 本文告诉大家如何在 ListView 或 ListBox 使用 WrapPanel 让里面的控件自动换行 在 WPF 可以通过修改 ItemsPanel 设置使用不同的 I ...
- java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized
Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä ...
- [Angular] Short Imports with TypeScript Path Mapping
The idea is change from: import {CoreModule} from '../core/core.module'; to: import {CoreModule} fro ...
- 【Android】把外部文件拷贝的AVD安卓模拟器上的sdcard上,而且在AVD中浏览sdcard的文件
首先.实现这一切的大前提是.你的AVD安卓模拟器,在启动之前.有设置好sdcard的大小,例如以下图.同一时候,你的AVD安卓模拟器,要处于启动状态.否则无法运行例如以下的操作. 这里以<[An ...
- 建筑建模学习笔记2——3DMax房屋框架建模
以下这幅图是用3DMax做出的大体的框架 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I ...