1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CF954D Fight Against Traffic.cpp 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#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;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385