ColinKIA @ 2023-02-28 18:38:16
经过不懈努力,在spfa的各种优化下,成功获得了84分,求更nb的优化
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
#define int long long
using namespace std;
const int MAXN=2e5+5;
template<typename T_>
struct stack{
T_ S[MAXN];
int tot=0;
void push(T_ a){
S[++tot]=a;
//if(S[tot]>S[1]) swap(S[tot],S[1]);
if(median()>S[tot]) swap(S[tot/2+1],S[tot]);
}
T_ top(){
return S[tot];
}
void pop(){
tot--;
}
void Ssort(){
sort(S+1,S+1+tot,greater<int>());
}
bool empty(){
return (tot==0?1:0);
}
int size(){
return tot;
}
int median(){
if(tot%2){
return S[tot/2+1];
}else{
return (S[tot/2]+S[tot/2+1])/2;
}
}
};
struct node{
int to,val;
node(){}
node(int T,int V){
to=T,val=V;
}
};
vector<node> G[MAXN];
void add(int a,int b,int c){
G[a].push_back(node(b,c));
}
int n,m,s,vis[MAXN],tim[MAXN],dis[MAXN];
void SPFA(int S){
stack<int> q;
for(int i=1;i<=MAXN;i++){
dis[i]=0x3f3f3f3f3f3f3f;
}
q.push(s);
dis[S]=0;
vis[S]=1;
tim[S]++;
while(!q.empty()){
if(rand()%10000==0) q.Ssort();
int now=q.top();
q.pop();
vis[now]=0;
int len=G[now].size();
for(int i=0;i<len;i++){
int y=G[now][i].to,w=G[now][i].val;
if(dis[y]>dis[now]+w){
dis[y]=dis[now]+w;
if(vis[y]==0){
vis[y]=1;
q.push(y);
tim[y]++;
}
}
}
}
bool flag=0;
for(int i=1;i<=n;i++){
printf("%lld ",dis[i]);
}
}
signed main(){
srand(time(0));
scanf("%lld %lld %lld",&n,&m,&s);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%lld %lld %lld",&a,&b,&c);
add(a,b,c);
}
SPFA(s);
}
验证码:g8tg祭
by HopesandDreams @ 2023-02-28 18:41:05
@ColinKIA 可以看看
by HopesandDreams @ 2023-02-28 18:41:49
@ColinKIA 用SPFA的某种优化是可以通过这题的,我教练试过,但我忘了是哪种了
by ColinKIA @ 2023-02-28 18:50:25
@114514YC 《关注博主》
(现在在学校,登不了csdn)
by Eznibuil @ 2023-02-28 18:53:03
@ColinKIA 魔改版的 SPFA 的确可以通过,但是我个人认为不是费用流却写 SPFA 不写 Bellman-Ford 纯属浪费。轻喷。
by Xy_top @ 2023-02-28 19:26:59
@ColinKIA 开手写版指令集优化
by HopesandDreams @ 2023-02-28 20:52:02
@ColinKIA 完了,没细看
by ben090302 @ 2023-03-02 23:41:23
@ColinKIA 概率设成和队长度有关的,不要用栈用队列(未知原因队列快一些)
by ben090302 @ 2023-03-03 17:50:52
@ben093002 e可以我傻了,栈可以只是注意排序和队列版本反的