CodeForces - 954D Fight Against Traffic

Fight Against Traffic

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 nms and t (2?≤?n?≤?1000, 1?≤?m?≤?1000, 1?≤?s,?t?≤?ns?≠?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?≤?nui?≠?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 51 22 33 44 5

Output

0

Input

5 4 3 51 22 33 44 5

Output

5

Input

5 6 1 51 21 31 44 53 52 5

Output

3

题意:n个城镇,m条道路(无向 s(起点 t(终点 然后求建一条新路,s-t的最短路径不会改变。思路:跑两次spfa s到各个点的最短路 t到各个点的最短路 然后枚举每次没有路的点 然后判断dis1[i]+dis2[j]+1>=dis1[t]&&dis1[j]+dis2[i]+1>=dis1[t],这样就cnt++

#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn= 1000+5;
const int INF = 0x3f3f3f3f;
int dis1[maxn],dis2[maxn];
int d[maxn][maxn],n,m;
bool vis[maxn];
void spfa(int s,int dis[1005])
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    vis[s]=1;
    dis[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        vis[v]=0;
        for(int i=1; i<=n; i++)
        {
            int w=d[v][i];
            if(w==INF)
                continue;
            if(dis[i]>dis[v]+w)
            {
                dis[i]=dis[v]+w;
                if(!vis[i])
                {
                    q.push(i);
                    vis[i]=1;
                }
            }
        }

    }

}
int main()
{
    int s,t,u,v;
    scanf("%d %d %d %d",&n,&m,&s,&t);
    memset(d,INF,sizeof(d));
    memset(dis1,INF,sizeof(dis1));
    memset(dis2,INF,sizeof(dis2));
    for(int i=1; i<=m; i++)
    {
        scanf("%d %d",&u,&v);
        d[u][v]=d[v][u]=1;
    }

    spfa(s,dis1);
    spfa(t,dis2);
    int cnt=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n&&i!=j; j++)
        {
            if(i==t&&j==s)
                continue;
            if((dis1[i]+dis2[j]+1>=dis1[t])&&(dis1[j]+1+dis2[i]>=dis1[t])&&d[i][j]==INF)
            {
                cnt++;
            }
        }
    cout<<cnt<<endl;

}

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~



原文地址:https://www.cnblogs.com/MengX/p/9119589.html

时间: 2024-10-11 09:24:50

CodeForces - 954D Fight Against Traffic的相关文章

CodeForces 487A Fight the Monster

1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int judge(int hy,int ay,int dy,int hm,int am,int dm)//计算特定的攻击与防御之下,需要加多少hp 5 { 6 if(am <= dy) 7 return 0; 8 int d1 = am - dy; 9 //cout<<" d1 = "<<d1<&

Codeforces - 102222H - Fight Against Monsters - 贪心

https://codeforc.es/gym/102222/problem/H 题意:有一堆怪兽,怪兽有HP和ATK.你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽.打的伤害递增,第一次1,第二次2,以此类推. 为什么感觉是贪心呢?证明一波. 首先开始打一个怪兽肯定一直打到死为止.那么打死他要求的次数可以二分出来(其实暴力也可以).两只怪兽交换打的顺序会不会变好? 先打第一只怪兽: \(num_1*sumatk+num_2*(sumatk-atk_1)\) 先打第二只怪兽: \(nu

Codeforces 1296D - Fight with Monsters

题目大意: n 只怪兽,每只的血量为 h[i] ,你的攻击力为 a ,你的对手攻击力为 b 打每只怪兽时,都是你先出手,然后你的对手出手,这样轮流攻击 如果是你给予了怪兽最后一击,你就能得到一分 你还有 k 次机会能让你的对手暂停行动一回合 问你最多能拿到多少分 解题思路: 记你加上你的对手两个人各攻击一次造成的伤害为 s=a+b 贪心可得,如果想节省那 k 次机会,应该和对手一起把怪兽耗到只剩一点血,即能在最后一回合杀死的情况时 即怪兽减去的血量为 s 的倍数,表示经过了这么多回合后,怪兽的血

Educational Codeforces Round 40 (Rated for Div. 2) Partial Solution

从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fight Against Traffic Problem E Water Taps Problem F Runner's Problem Problem G Castle Defense Problem H Path Counting Problem I Yet Another String Match

Educational Codeforces Round 40 (Rated for Div. 2)

这场没打啊 A. Diagonal Walking 签到 B. String Typing 签到 C. Matrix Walk 题意 分析 D. Fight Against Traffic 题意 分析   E. Water Taps 题意 分析 F. Runner's Problem 题意 分析 G. Castle Defense 题意 分析 原文地址:https://www.cnblogs.com/Superwalker/p/8641400.html

Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)

There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb hp. You and your opponent are fighting these

Codeforces Beta Round #5 D. Follow Traffic Rules

这个题是个高中的物理公式,只要细节处理好就能过(现在最怕的就是细节啊)~~   题目大意: 城市A.B之间有一条路,长度为l,在距离A城市d的位置放置了限速标志,意味着到那个点的时候速度必须不大于限制速度.现有一车在城市A出发,车的加速度为a,最大速度为v,求通过这条路的最小时间.车出发时的速度为零. 解题思路: 套物理公式,分类讨论 下面是代码: #include <set> #include <map> #include <queue> #include <m

Codeforces 498D Traffic Jams in the Land | 线段树

题目大意: 给坐标轴1~n的点,每个点有一个权值,从一个点走到下一个点需要1s,如果当前时间是权值的倍数就要多花1s 给出q组操作,C表示单点修改权值,A表示询问0时刻x出发到y的时间 题解:因为权值只有2,3,4,5,6,所以60是一个周期,我们维护一颗线段树,维护0到59时刻出发从l到r+1用的时间 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define N 100010 5 us

[codeforces] 498D Traffic Jams in th Land

原题 简单的线段树问题. 对于题目中,a[i]的范围是2~6,我们仔细思考可以得出第0秒和第60秒是一样的(因为2~6的最小公倍数是60,),然后我们可以建一个线段树,里面记录0~59秒时刻开始通过这段所需要的时间.(如果一定要说这是60棵线段树也不是不可以--) #include<cstdio> #define N 100010 using namespace std; int n,a[N],q,x,y; char j; struct hhh { int l,r,dt[65]; }tre[N