zoukankan      html  css  js  c++  java
  • CodeForces

    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 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的最短路径不会改变。
    思路:跑两次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;
    
    }
    View Code

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

    
    
  • 相关阅读:
    p3c安装使用 编码规范扫描 阿里巴巴出品,挺好用的
    Ideal test 不执行main方法了
    Maven 3-Maven依赖版本冲突的分析及解决小结
    (String)强制转换、toString()和String.valueOf()的区别
    Linux tail 命令详解
    iconv的安装和使用
    daemon函数的原理及使用详解
    SQL Sever 2012 如何建立数据库连接
    Navicat Premium 将sqlserver 数据库 导入mysql 中
    MySQL也有潜规则 – Select 语句不加 Order By 如何排序?
  • 原文地址:https://www.cnblogs.com/MengX/p/9119589.html
Copyright © 2011-2022 走看看