求助

P1342 请柬

fangyishen114514 @ 2024-09-15 14:43:17

WA前三个

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f

int n,m;

struct node
{
    int to,w;
};

vector<node> head[114514];
vector<node> head1[114514];

bool operator<(const node a,const node b)
{
    return a.w>b.w;
}

priority_queue<node> q;
priority_queue<node> q1;

int dis[114514],ans;

bool vis[114514],vis1[114514];

void dijkstra(int start)
{
    int i,j,k,x,y;
    for(i=1;i<=n;i++)
    {
        dis[i]=INF;
        vis[i]=false;
    } 
    dis[start]=0;
    vis[start]=true;
    for(i=0;i<head[start].size();i++)
    {
        k=head[start][i].to;
        dis[k]=head[start][i].w;
        q.push({k,dis[k]});
    }
    node temp;
    while(!q.empty())
    {
        temp=q.top();
        q.pop();
        x=temp.to;
        if(vis[x]) continue;
        vis[x]=true;
        for(i=0;i<head[x].size();i++)
        {
            y=head[x][i].to;
            if(vis[y]) continue;
            if(dis[x]+head[x][i].w<dis[y])
            {
                dis[y]=dis[x]+head[x][i].w;
                q.push({y,dis[y]});
            }
        }
    }
}

void dijkstra1(int start)
{
    int i,j,k,x,y;
    for(i=1;i<=n;i++)
    {
        dis[i]=INF;
        vis[i]=false;
    } 
    dis[start]=0;
    vis[start]=true;
    for(i=0;i<head1[start].size();i++)
    {
        k=head1[start][i].to;
        dis[k]=head1[start][i].w;
        q1.push({k,dis[k]});
    }
    node temp;
    while(!q1.empty())
    {
        temp=q1.top();
        q1.pop();
        x=temp.to;
        if(vis[x]) continue;
        vis[x]=true;
        for(i=0;i<head1[x].size();i++)
        {
            y=head1[x][i].to;
            if(vis[y]) continue;
            if(dis[x]+head1[x][i].w<dis[y])
            {
                dis[y]=dis[x]+head1[x][i].w;
                q1.push({y,dis[y]});
            }
        }
    }
}

int main()
{
    int i,j,k,ans;
    cin>>n>>m;
    for(i=1;i<=m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        head[u].push_back({v,w});
        head1[v].push_back({u,w});
    }
    dijkstra(1);
    for(i=2;i<=n;i++) ans+=dis[i];
    dijkstra1(1);
    for(i=2;i<=n;i++) ans+=dis[i];
    cout<<ans-1;
    return 0;
}

by zhouzl @ 2025-01-09 22:50:06

我和你一样


by zevan @ 2025-01-11 10:27:30

不开long long见祖宗


|