您的位置:首页 > 大数据 > 人工智能

Educational Codeforces Round 40 (Rated for Div. 2) D. Fight Against Traffic(SPFA)

2018-03-28 17:59 483 查看
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
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

Copy

5 4 1 5

1 2

2 3

3 4

4 5

Output
0

Input

Copy

5 4 3 5

1 2

2 3

3 4

4 5

Output
5

Input

Copy

5 6 1 5

1 2

1 3

1 4

4 5

3 5

2 5

Output
3

题意:
给你一张图,和一个起点s,终点t,问你有多少个点对,使得点对本身连接起来之后,s->t的距离不会减少

解析:
大佬给的思路,先从起点spfa一遍得到d[],再从终点spfa一遍得到dt[],从中可以得到s->t的距离m,之后对每一个点对i,j进行找答案,若s->i->j->t的距离小于m&&s->j->i->t,则ans++
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f

const int MAXN =1e3+10;

int mp[MAXN][MAXN];
int d[MAXN],n;
int dt[MAXN];
int vis[MAXN],pre[MAXN];

void SPFA(int src,int d[]){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) d[i]=INF,pre[i]=i;
d[src]=0;
queue<int> q;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=1;i<=n;i++)
{
if(i==u) continue;
if(mp[u][i]!=-1)
{
int tmp=d[u]+mp[u][i];
if(d[i]>tmp){
d[i]=tmp;
pre[i]=u;
if(!vis[i]){
vis[i]=true;
q.push(i);
}
}

}
}

}
}

int main()
{
int m,s,t;
while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF)
{
memset(mp,-1,sizeof(mp));
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=1;
mp[v][u]=1;
}

SPFA(s,d);
SPFA(t,dt);
memset(vis,0,sizeof(vis));
int xx=s;
while(xx!=pre[xx])
{
vis[xx]=1;
xx=pre[xx];
}
if(xx!=s) vis[xx]=1;
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(mp[i][j]==1) continue;
if(vis[i]&&vis[j]) continue;
if(i==s)
{
if(dt[j]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(i==t)
{
if(d[j]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(j==s)
{
if(dt[i]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(j==t)
{
if(d[i]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(d[i]+dt[j]+1>=d[t]&&d[j]+dt[i]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
}

printf("%d\n",ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐