样例不过求调

P1342 请柬

danlao @ 2024-05-04 16:35:49

#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inline")
#define hh putchar('\n')
#define kg putchar(' ')
#define debug puts("debug")
//#define int long long
//#define int __int128
namespace quickread{
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void write(int x){
        if(x<0){putchar('-');x=-x;}
        if(x>9)write(x/10);
        putchar(x%10+'0');
    }
    inline string readstr(){
        char ch=getchar();string str="";
        while(!(ch>='0'&&ch<='9')&&!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')) ch=getchar();
        while((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){str+=ch;ch=getchar();}
        return str;
    }
    inline char readchar(){
        char ch=getchar();
        while(!(ch>='0'&&ch<='9')&&!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')) ch=getchar();
        return ch;
    }
}
using namespace quickread;
const int N=1e6+10;
int n,m,dis[N],ans;
bool vis[N];
struct edge{
    int v,w;
};
vector<edge>p[N],g[N];
struct csp{
    int u,w;
};
bool operator > (const csp& a,const csp& b){
    return a.w>b.w;
}
void dijkstra_p(int u){
    memset(dis,0x3f,sizeof(dis));memset(vis,0,sizeof(vis));
    dis[u]=0;
    priority_queue<csp ,vector<csp> ,greater<csp> >q;
    q.push(csp{u,0});
    while(!q.empty()){
        u=q.top().u;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(edge to:p[u]){
            int v=to.v,w=dis[u]+to.w;
            if(dis[v]>w){
                dis[v]=w;
                q.push(csp{v,w});
            }
        }
    }
    for(int i=2;i<=n;i++)
        ans+=dis[i];
}
void dijkstra_g(int u){
    memset(dis,0x3f,sizeof(dis));memset(vis,0,sizeof(vis));
    dis[u]=0;
    priority_queue<csp ,vector<csp> ,greater<csp> >q;
    q.push(csp{u,0});
    while(!q.empty()){
        u=q.top().u;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(edge to:g[u]){
            int v=to.v,w=dis[u]+to.w;
            if(dis[v]>w){
                dis[v]=w;
                q.push(csp{v,w});
            }
        }
    }
    for(int i=2;i<=n;i++)
        ans+=dis[i];
}
signed main(){
//  ios::sync_with_stdio(0);
//  cin.tie(0);cout.tie(0);
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        int u=read(),v=read(),w=read();
        p[u].push_back(edge{v,w});
        g[v].push_back(edge{u,w});
    }
    dijkstra_p(1);
    dijkstra_g(1);
    write(ans);
    return 0;
}

by Kindershiuo @ 2024-06-24 14:16:18

方向建边跑一遍dijkstra

正向建边再跑一遍dijkstra就好了


by Kindershiuo @ 2024-06-24 14:16:34

@Kindershiuo 反向


|