codeforces 954D dijkstra变形

Codeforces 954D

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
intput
5 4 1 5
1 2
2 3
3 4
4 5
output
0
intput
5 4 3 5
1 2
2 3
3 4
4 5
output
5
intput
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
output
3
解题思路

一道典型的dijsktra算法题,先用两次dijsktra分别算出原点s和终点t到其他各点的距离,求出distance(s,t),及s与t的距离。再判断每一对未直接相邻的点,及 if(d1[i]+d2[j]+1>=num && d1[j]+d2[i]+1>=num)。满足条件,则数量cnt加一。最后输出总数cnt;

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <cstring>
#include <algorithm>
#include <sstream>
#define INF 1000000
#define MAXN 2e5+9
const long long mod=1e6+3;
using namespace std;
int g[1005][1005];
//int via[1005][1005];
int via[1005];
int d1[1005],d2[1005];
int s,t,n,m;
void disk(int* d,int k)
{
    memset(via,0,sizeof(via));
    int i,j;
    int x,y;
    d[k]=0;
    for(i=0;i<n;i++)
    {
        int mmax=INF;
        for(j=1;j<=n;j++)
        {
            if(via[j]) continue;
            if(d[j]<mmax)
            {
                mmax=d[j];
                x=j;
            }
        }
        via[x]=1;
        for(j=1;j<=n;j++)
        {
            if(j==x) continue;
            if(!g[x][j]) continue;
            if(d[x]+1<d[j])
            {
                d[j]=d[x]+1;
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&s,&t);
    int i,j;
    int x,y;
    memset(g,0,sizeof(g));
    memset(d2,INF,sizeof(d2));
    memset(d1,INF,sizeof(d1));
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        g[x][y]=g[y][x]=1;
    }
    //cout << 1 << endl;
    disk(d1,s);
    disk(d2,t);
    /*for(i=1;i<=n;i++)
        cout << d1[i]<<' ';
    cout << endl;
    for(i=1;i<=n;i++)
        cout << d2[i]<<' ';
    cout << endl;*/
    int num=d1[t];
    int cnt=0;
    //cout << num << endl;
    for(i=1;i<n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if((i==s && j==t) || (i==t && j==s)) continue;
            if(g[i][j]) continue;
            if(d1[i]+d2[j]+1>=num && d1[j]+d2[i]+1>=num)
            {
                cnt++;
                //cout <<d1[i] <<' ' <<d2[j] << endl;
                //cout <<i <<' ' <<j << endl;
            }
        }
    }
    printf("%d\n",cnt);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值