Codeforces 954D Fight Against Traffic(最短路)

Description

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

题目大意

n个顶点m条边的无向图,现要求添加一条边并且不改变s->t的最短路,问有多少种可行的情况.

解题思路

求出以s为源点到所有顶点的最短路,和以t为源点到所有顶点的最短路. 在加边的过程中可以选择两个不相连的顶点进行建边,则s->t 便转化为s->i->j->t或者s->j->i->t;即dis[s][i]+dis[j][t]+1和dis[s][j]+dis[i][t]+1,选择较小值和最短路比较即可.

代码实现

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
const int maxn =1007;
#define INF 0x3f3f3f3f
int path[maxn];
int maps[maxn][maxn];
struct node
{
    int u;
    int v;
    int cost;
    int next;
} edge[maxn*2];
int head[maxn],tot;
int vis[maxn];
int diss[maxn],dist[maxn];
void init()
{
    memset(maps,0,sizeof(maps));
    memset(head,-1,sizeof(head));
    tot=0;
}
void addedge(int u,int v,int c)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].cost=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void spfa(int s,int *dis)
{
    memset(dis,INF,sizeof(int)*maxn);
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    vis[s]=1;
    queue<int>qu;
    qu.push(s);
    while(!qu.empty())
    {
        int u=qu.front();
        qu.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                if(!vis[v])
                {
                    vis[v]=1;
                    qu.push(v);
                }
            }
        }
    }
}
int main()
{
    IO;
    int n,m,s,t;
    int u,v;
    cin>>n>>m>>s>>t;
    init();
    for(int i=0; i<m; i++)
    {
        cin>>u>>v;
        maps[u][v]=1;
        maps[v][u]=1;
        addedge(u,v,1);
        addedge(v,u,1);
    }
    spfa(t,dist);
    spfa(s,diss);
//    for(int i=1;i<=n;i++)
//        cout<<diss[i]<<endl;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(!maps[i][j])
            {
                int a1=diss[i]+dist[j]+1;
                int a2=dist[i]+diss[j]+1;
                if(min(a1,a2)>=diss[t])
                {
                    ans++;
//                    if(a1<a2)
//                        cout<<s<<" "<<i<<" "<<j<<" "<<t<<endl;
//                    else
//                        cout<<s<<" "<<j<<" "<<i<<" "<<t<<endl;
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值