zoukankan      html  css  js  c++  java
  • Codeforces 954 D Fight Against Traffic

    Discription

    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.

    Example

    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


    两遍dfs之后暴力判断即可。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=1005;
    bool a[maxn][maxn],v[maxn];
    int n,m,S,T,d[maxn],g[maxn],ans;
    int to[maxn*2],ne[maxn*2],hd[maxn];
    
    inline void BFS(){
    	queue<int> q; int x;
    	q.push(S),v[S]=1;
    	while(!q.empty()){
    		x=q.front(),q.pop();
    		for(int i=hd[x];i;i=ne[i]) if(!v[to[i]]){
    			v[to[i]]=1,d[to[i]]=d[x]+1;
    			q.push(to[i]);
    		}
    	}
    	
    	memset(v,0,sizeof(v));
    	q.push(T),v[T]=1;
    	while(!q.empty()){
    		x=q.front(),q.pop();
    		for(int i=hd[x];i;i=ne[i]) if(!v[to[i]]){
    			v[to[i]]=1,g[to[i]]=g[x]+1;
    			q.push(to[i]);
    		}
    	}	
    }
    
    int main(){
    	scanf("%d%d%d%d",&n,&m,&S,&T);
    	int uu,vv;
    	for(int i=1;i<=m;i++){
    		scanf("%d%d",&uu,&vv),a[uu][vv]=a[vv][uu]=1;
    		to[i]=vv,ne[i]=hd[uu],hd[uu]=i;
    		to[i+m]=uu,ne[i+m]=hd[vv],hd[vv]=i+m;
    	}
    	
    	BFS();
    	
    	for(int i=1;i<=n;i++)
    	    for(int j=i+1;j<=n;j++) if(!a[i][j])
    	        if(d[i]+g[j]>=d[T]-1&&d[j]+g[i]>=d[T]-1) ans++;
    	
    	printf("%d
    ",ans);
    	return 0;
    }
    

      

     
  • 相关阅读:
    PKI的签密体制学习(含信息安全基础)
    Linux软件安装
    Linux基础
    JSP EL表达式入门1
    Tomcat 错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099 解决方法
    JSP JSTL入门
    JSP组件
    JSP动作
    实现序列化的java类中的serialVersionUID的作用
    [Noip2016]蚯蚓
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8672525.html
Copyright © 2011-2022 走看看