Educational Codeforces Round 40 D. Fight Against Traffic(最短路)

D. Fight Against Traffic
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

Examples
inputCopy
5 4 1 5
1 2
2 3
3 4
4 5
output
0
inputCopy
5 4 3 5
1 2
2 3
3 4
4 5
output
5
inputCopy
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
output
3
题意: 给你一个无向图,问你加一条边使得 s->t的最短路径不变
分析: 因为图的大小为1e3,所以我们暴力的判断每条边即可,我们先预处理出来s和t点到其他个点的最短路径,然后判的时候只需保证以下:
我们假设当前枚举增加i节点和j节点之间的边
满足s -> i + i -> j + j -> t >= s -> t
和s -> j + j -> i + i -> t >= s -> t,答案就可以++

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+100;
int arr[N][N];
vector<int>G[N];
int vis1[N],vis2[N];
int main()
{
    //cout << "Hello world!" << endl;
    ios::sync_with_stdio();
    int n,m,s,t;
    cin>>n>>m>>s>>t;
    memset(vis1,-1,sizeof vis1);
    memset(vis2,-1,sizeof vis2);
    for(int i=0;i<m;i++)
    {
        int a,b;
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
        arr[a][b]=arr[b][a]=1;
    }
    queue<int>q;
    q.push(s);
    vis1[s]=0;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int i=0;i<G[v].size();i++)
        {
            int y=G[v][i];
            if(vis1[y]==-1)
            {
                vis1[y]=vis1[v]+1;
                q.push(y);
            }
        }
    }
    q.push(t);
    vis2[t]=0;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int i=0;i<G[v].size();i++)
        {
            int y=G[v][i];
            if(vis2[y]==-1)
            {
                vis2[y]=vis2[v]+1;
                q.push(y);
            }
        }
    }
    ll res=0;
    for(int i=1;i<=n;i++)
    {

        for(int j=i+1;j<=n;j++)
        {
            if(!arr[i][j]&&vis1[i]+vis2[j]+1>=vis1[t]&&vis1[j]+vis2[i]+1>=vis1[t])
                res++;
        }
    }
    cout<<res<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值