Codeforces 954D. Fight Against Traffic (思维)

                                                                              D. Fight Against Traffic

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nms and t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, 1 ≤ s, t ≤ ns ≠ 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 ≤ nui ≠ 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.

Examples

input

Copy

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

output

Copy

0

input

Copy

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

output

Copy

5

input

Copy

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

output

Copy

 

一、原题地址

点我传送

 

二、大致题意

给出一个双向边的连通图(不存在重边),现在要求给图上的点连边,要求保证连上这样一条边后s到t的最短距离不会改变。求最多有多少种可以连的边。

 

三、思路

以s为起点跑的bfs用d[ ]来记录s到图上每个点的最短路,以t为起点的bfs用dd[ ]来记录t点到图上每个点的最短路。

这样我们只要n^2枚举图上的所有点,保证d[ i ]+dd[ j ]再加上一步从 i 到 j 的距离是大于等于d [ t ]的,这样就保证了d[ t ]不会发生改变。

 

四、代码

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <iterator>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;


int n, m, s, t;
vector<int>e[1005];
int d[1005], dd[1005];
bool mmp[1005][1005];
void bfs(int st,int *dis)
{
	for (int i = 1; i <= n; i++)dis[i] = inf;
	bool vis[1005];
	queue<int>q;
	memset(vis, false, sizeof(vis));
	vis[st] = true;
	dis[st] = 0;
	q.push(st);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		int size = e[t].size();
		for (int i = 0; i < size; i++)
		{
			int to = e[t][i];
			if (!vis[to])
			{
				q.push(to);
				vis[to] = true;
				dis[to] = dis[t] + 1;
			}
		}
	}
}
void read()
{
	scanf("%d %d %d %d", &n, &m, &s, &t);
	memset(mmp, false, sizeof(mmp));
	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		e[u].push_back(v);
		e[v].push_back(u);
		mmp[u][v] = mmp[v][u] = true;
	}
}
int main()
{
	read();
	bfs(s,d);
	bfs(t, dd);
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		for (int j = i + 1; j <= n; j++)
		{
			if (d[j] + dd[i] + 1 >= d[t] &&
				d[i] + dd[j] + 1 >= d[t] &&
				(!mmp[i][j]))ans++;
		}
	}
	cout << ans << endl;
	getchar();
	getchar();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值