代码拉取完成,页面将自动刷新
同步操作将从 charlieshu/Algorithms 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include <iostream>
#include <queue>
using namespace std;
struct edge{
int stop,next;
};
bool etf(int *e_header,edge *a,int start,int stop){
int l;
l = e_header[start];
while(l != -1){
if(a[l].stop == stop)
return true;
l = a[l].next;
}
return false;
}
//void log(int *d, int n){
// for(int i=0;i<n;i++) cout<<d[i]<<" ";
// cout<<endl;
//}
int main(){
int n,m,s,t;
int inf = 1e4;
cin>>n>>m>>s>>t;
s--;
t--;
edge edges[2*m];
bool tf_s[n],tf_t[n];
queue<int> q;
int e_header[n];
int fast_s[n],fast_t[n];
for(int i=0;i<n;i++){
e_header[i] = -1;
tf_s[i] = true;
tf_t[i] = true;
fast_s[i] = inf;
fast_t[i] = inf;
}
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
u--;
v--;
edges[i].stop = v;
edges[i].next = e_header[u];
e_header[u] = i;
edges[i+m].stop = u;
edges[i+m].next = e_header[v];
e_header[v] = i+m;
}
//从起点s开始的单元点最短路径
tf_s[s] = false;
q.push(s);
fast_s[s] = 0;
while(!q.empty()){
int l,len,stop;
l = q.front();
len = fast_s[l];
tf_s[l] = true;
q.pop();
stop = e_header[l];
while(stop != -1){
if(fast_s[edges[stop].stop] > len+1){
fast_s[edges[stop].stop] = len+1;
if(tf_s[edges[stop].stop]){
q.push(edges[stop].stop);
tf_s[edges[stop].stop] = false;
}
}
stop = edges[stop].next;
}
}
//从终点t开始的单元点最短路径
tf_t[t] = false;
q.push(t);
fast_t[t] = 0;
while(!q.empty()){
int l,len,stop;
l = q.front();
len = fast_t[l];
tf_t[l] = true;
q.pop();
stop = e_header[l];
while(stop != -1){
if(fast_t[edges[stop].stop] > len+1){
fast_t[edges[stop].stop] = len+1;
if(tf_t[edges[stop].stop]){
q.push(edges[stop].stop);
tf_t[edges[stop].stop] = false;
}
}
stop = edges[stop].next;
}
}
int num=0;
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
if(fast_s[i]+fast_t[j]+1 >= fast_s[t] && fast_s[j]+fast_t[i]+1 >= fast_s[t] && !etf(e_header,edges,i,j))
num++;
cout<<num;
// cout<<endl<<"fast_s: "<<endl; log(fast_s, n);
// cout<<"fast_t: "<<endl; log(fast_t, n);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。