CodeForces - 954D Fight Against Traffic(最短路,dijkstra)

2023-10-05 23:18

本文主要是介绍CodeForces - 954D Fight Against Traffic(最短路,dijkstra),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

描述

Little town Nsk consists of n junctions connected by m bidirectional
roads. Each road connects two distinct junctions and no two roads
connect the same pair of junctions. It is possible to get from any
junction to any other junction by these roads. The distance between
two junctions is equal to the minimum possible number of roads on a
path between them.

In order to improve the transportation system, the city council asks
mayor to build one new road. The problem is that the mayor has just
bought a wonderful new car and he really enjoys a ride from his home,
located near junction s to work located near junction t. Thus, he
wants to build a new road in such a way that the distance between
these two junctions won’t decrease.

You are assigned a task to compute the number of pairs of junctions
that are not connected by the road, such that if the new road between
these two junctions is built the distance between s and t won’t
decrease.

Input

The firt line of the input contains integers n, m, s and t
(2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, 1 ≤ s, t ≤ n, s ≠ t) — the number of
junctions and the number of roads in Nsk, as well as the indices of
junctions where mayors home and work are located respectively. The
i-th of the following m lines contains two integers ui and vi
(1 ≤ ui, vi ≤ n, ui ≠ vi), meaning that this road connects junctions
ui and vi directly. It is guaranteed that there is a path between any
two junctions and no two roads connect the same pair of junctions.

Output

Print one integer — the number of pairs of junctions not connected by
a direct road, such that building a road between these two junctions
won’t decrease the distance between junctions s and t.

input

5 4 1 5
1 2
2 3
3 4
4 5

output

0

input

5 4 3 5
1 2
2 3
3 4
4 5

output

5

input

5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5

output

3

思路

这道题给了n个点和m条边,还给了一个起点和终点,问最多加多少条边,可以使得起点到终点的最短路不会变小

首先我们可以知道,在有n个点的图中,最多有 n(n1)2 n ( n − 1 ) 2 条边,我们可以首先从起点和终点分别跑一下最短路,运用dijkstra算法,假设从起点开始的最短路是dis1[]数组,从终点开始的是dis2[]数组,那么我们可以枚举一下图中没有出现过的边,然后判断是否满足条件:
dis1[i]+1+dis2[j]>=dis1[ed]dis2[i]+1+dis1[j]>=dis2[st]就证明这一条边的加入不会影响从起点到终点的最短路,那么就证明这一条边可以加入

代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=1000+20;
int e[N][N];
int n,m;
void init()
{for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)e[i][j]=i==j?0:inf;
}
struct Dijkstra
{int vis[N],dis[N];void dijkstra(int st){for(int i=1; i<=n; i++){dis[i]=inf;vis[i]=0;}dis[st]=0;for(int i=1; i<=n; i++){int minn=inf,k;for(int j=1; j<=n; j++)if(!vis[j]&&dis[j]<minn){minn=dis[j];k=j;}vis[k]=1;for(int j=1; j<=n; j++)if(!vis[j]&&dis[k]+e[k][j]<dis[j])dis[j]=dis[k]+e[k][j];}}
} ac1,ac2;
int main()
{int u,v,st,ed;scanf("%d%d%d%d",&n,&m,&st,&ed);init();for(int i=1; i<=m; i++){scanf("%d%d",&u,&v);e[u][v]=e[v][u]=1;}ac1.dijkstra(st);ac2.dijkstra(ed);int ans=0;for(int i=1; i<=n; i++)for(int j=i+1; j<=n; j++)if(e[i][j]==inf)if(ac1.dis[i]+1+ac2.dis[j]>=ac1.dis[ed]&&ac2.dis[i]+1+ac1.dis[j]>=ac2.dis[st])ans++;printf("%d\n",ans);return 0;
}

这篇关于CodeForces - 954D Fight Against Traffic(最短路,dijkstra)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/152269

相关文章

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10801(乘电梯dijkstra)

题意: 给几个电梯,电梯0 ~ n-1分别可以到达很多层楼。 换乘电梯需要60s时间。 问从0层到target层最小的时间。 解析: 将进入第0层的电梯60s也算上,最后减。 坑点是如果target为0输出0。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algori

poj 3159 (spfa差分约束最短路) poj 1201

poj 3159: 题意: 每次给出b比a多不多于c个糖果,求n最多比1多多少个糖果。 解析: 差分约束。 这个博客讲差分约束讲的比较好: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 套个spfa。 代码: #include <iostream>#include <cstdio>#i

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

poj 3255 次短路(第k短路) A* + spfa 或 dijkstra

题意: 给一张无向图,求从1到n的次短路。 解析: A* + spfa 或者 dijkstra。 详解见上一题:http://blog.csdn.net/u013508213/article/details/46400189 本题,spfa中,stack超时,queue的效率最高,priority_queue次之。 代码: #include <iostream>#i

poj 2449 第k短路 A* + spfa

poj 2449: 题意: 给一张有向图,求第k短路。 解析: A* + spfa。 一下转自:http://blog.csdn.net/mbxc816/article/details/7197228 “描述一下怎样用启发式搜索来解决K短路。 首先我们知道A*的基础公式:f(x)=g(x)+h(x);对h(x)进行设计,根据定义h(x)为当前的x点到目标点t所需要的实际距

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m