cf 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

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

中文:

给你一个无向图,然后给你一个起点s和终点t。现在让你给这个无向图添加一个边,使得s到t之间的最短路径不会发生改变。 问你有多少种添加边的方法。边的权值都是1

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn=1001;
int G[maxn][maxn];
int mark[maxn][maxn];
int n,m,s,t;

void floyed()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(G[i][k]!=INT_MAX&&G[k][j]!=INT_MAX)
                    G[j][i]=G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
            }
        }
    }
}


int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m>>s>>t)
    {
        memset(G,0,sizeof(G));
        memset(mark,false,sizeof(mark));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                G[i][j]=G[j][i]=INT_MAX;
            G[i][i]=0;
        }
        int a,b;
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b;
            G[a][b]=G[b][a]=1;
            mark[a][b]=mark[b][a]=1;
        }

        mark[s][t]=mark[t][s]=1;
        floyed();
        //cout<<G[s][t]<<endl;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(!mark[i][j])
                {
                    if(G[s][i]+G[j][t]+1>=G[s][t]&&G[t][i]+G[j][s]+1>=G[s][t])
                        ans++;
                }
            }
        }
        cout<<ans<<endl;
    }


    return 0;
}

思路:

思路很简单,先用floyed算法计算出所有节点之间的最短路,然后枚举所有图中不存在的边,利用floyed算法中的性质,如果找到了一条边(i,j),想要判断连接(i,j)后s到t的距离是否有改变,设G[a][b]表示节点a到节点b的最短路。

那么,只要判断s到i的距离,加上j到t的距离,再加上新添加的那个边,也就是1,结果是否大于等于s到t的距离即可。
即G[s][i]+G[j][t]+1大于等于G[s][t]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值