954D - Fight Against Traffic - CodeForces Solution


dfs and similar graphs shortest paths *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define fi first
#define se second
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
#define yes {cout<<"Yes"<<endl;return;}
#define no {cout<<"No"<<endl;return;}
#define cntbit __builtin_popcountll
#define pii pair<int,int>
#define vii vector<pair<int,int>>
#define all(x) (x).begin(), (x).end()
using namespace std;
const int N = 1010;
const int inf = 1e18;
const int mod = 1e9+7;
void add(int &a,int b){
	a+=b;
	if(a>=mod) a-=mod;
}
int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}
int qmi(int a,int b){
	int res=1;
	for(;b;b>>=1){
		if(b&1) res=res*a%mod;
		a=a*a%mod; 
	}return res;
}
int n,m,s,t; vector<int> e[N];
vector<vector<int>> dis;
void bfs(int s){
	dis[s][s]=0;
	queue<int> q;
	q.push(s);
	while(q.size()){
		int u=q.front();
		q.pop();
		for(auto v:e[u]){
			if(dis[s][v]>dis[s][u]+1){
				q.push(v);
				dis[s][v]=dis[s][u]+1;
			}
		}
	}
}
int edge[N][N];
void solve(){
	cin>>n>>m>>s>>t;
	dis=vector<vector<int>> (n+1,vector<int> (n+1,inf));
	int ans=0;
	while(m--){
		int u,v;cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
		edge[u][v]=1;
		edge[v][u]=1;
	}
	rep(i,1,n+1) bfs(i);
	rep(i,1,n+1) rep(j,i+1,n+1){//won't dec
		if(edge[i][j]) continue;
		int cond1=((dis[s][i]+1+dis[j][t])>=dis[s][t]);
		int cond2=((dis[s][j]+1+dis[i][t])>=dis[s][t]);
		cond1&=cond2;
		if(cond1) ans++;
	}
	cout<<ans<<endl;
	
	
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	cout<<fixed<<setprecision(12);
	int task=1;//cin>>task;
	rep(i,0,task){
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

505B - Mr Kitayuta's Colorful Graph
1324D - Pair of Topics
157B - Trace
34C - Page Numbers
279A - Point on Spiral
1294D - MEX maximizing
447A - DZY Loves Hash
23B - Party
63D - Dividing Island
1203E - Boxers
1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings