dijkstra,WA前三个,求调

P1342 请柬

apple_vinegar @ 2024-07-20 10:57:23

#include<bits/stdc++.h>
using namespace std;
const int N=2*1e6+5;
long long ans,dist[N],dist1[N],w[N],w1[N];
int a,b,c,idx,e[N],ne[N],h[N],e1[N],ne1[N],h1[N],v[N],n,m;

void dij1(){
    memset(dist,0x3f3f3f3f,sizeof dist);
    priority_queue<pair<long long,long long> > heap;
    dist[1]=0;
    heap.push(make_pair(0,1));
    while(!heap.empty()){
        long long x=heap.top().second;
        heap.pop();
        if(v[x]==1) continue;
        v[x]=1;
        for(int i=h[x];i!=-1;i=ne[i]){
            int j=e[i];
            if(dist[j]>dist[x]+w[i]) {
                dist[j]=dist[x]+w[i];
                heap.push(make_pair(dist[j],j));
            }
        }
    }
}
void dij2(){
    memset(v,0,sizeof v);
    memset(dist1,0x3f3f3f3f,sizeof dist1);
    priority_queue<pair<long long,long long> > heap;
    dist1[1]=0; 
    heap.push(make_pair(0,1));
    while(!heap.empty()){

        long long x=heap.top().second;
        heap.pop();
        if(v[x]==1) continue;
        v[x]=1;
        for(int i=h1[x];i!=-1;i=ne1[i]){
            int j=e1[i];
            if(dist1[j]>dist1[x]+w1[i]) {
                dist1[j]=dist1[x]+w1[i];
                heap.push(make_pair(dist[j],j));
            }
        }
    }
}

void add(int a,int b,int c){
    w[idx]=c;
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;

}

void aad(int b,int a,int c){
    w1[idx]=c;
    e1[idx]=b;
    ne1[idx]=h1[a];
    h1[a]=idx++;
}

int main(){
    memset(h,-1,sizeof h);
    memset(h1,-1,sizeof h1);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        aad(a,b,c);
    }
    dij1();
    idx=0;
    dij2();
    for(int i=2;i<=n;i++){
        ans+=dist[i];
        ans+=dist1[i];
    }
    cout<<ans;
    return 0;
}

by qnqfff @ 2024-07-20 11:08:06

if(dist1[j]>dist1[x]+w1[i]) {
                dist1[j]=dist1[x]+w1[i];
                heap.push(make_pair(dist[j],j));
            }

--->

if(dist1[j]>dist1[x]+w1[i]) {
                dist1[j]=dist1[x]+w1[i];
                heap.push(make_pair(dist1[j],j));
            }

by apple_vinegar @ 2024-07-20 11:10:06

@qnqfff 大佬,改了后还是WA三个,可能是发讨论发错了


by qnqfff @ 2024-07-20 11:14:12

还有,你小根堆搞成大根堆了


by qnqfff @ 2024-07-20 11:14:56

priority_queue<pair<long long,long long> > heap;

--->

priority_queue<pair<long long,long long>,vector<pair<long long,long long> >,greater<pair<long long,long long> > > heap;

by apple_vinegar @ 2024-07-20 11:20:25

已A,已关,多谢


|