求助,只有 24 pts
查看原帖
求助,只有 24 pts
239192
淸梣ling楼主2023/3/18 16:32

思路没什么问题,但是只有 24 pts,其余全 WA,请问是什么原因 qwq

#include<bits/stdc++.h>
using namespace std;

vector< pair<int,int> > v[25100],vv[25100];
vector<int> b[25100],c[25100];
int fa[25100],rd[25100];
long long dis[25100];
queue<int> q;

int find(int x) { return x==fa[x] ? fa[x] : fa[x]=find(fa[x]); }
void dij(int x)
{
    priority_queue< pair<long long,int> > pq;
    for(int y : b[x]) pq.emplace(-dis[y], y);
    while(pq.size())
    {
        long long d=-pq.top().first;
        int x=pq.top().second; pq.pop();
        if(d!=dis[x]) continue;
        for(pair<int,int> e : v[x])
        if(dis[e.first]>dis[x]+e.second)
        {
            dis[e.first]=dis[x]+e.second;
            pq.emplace(-dis[e.first], e.first);
        }
    }
}
int main()
{
    // freopen("P3008_2.in", "r", stdin);
    // freopen("a.out", "w", stdout);
    int n,m1,m2,s;

    cin>>n>>m1>>m2>>s;
    for(int i=1; i<=n; i++) fa[i]=i;
    for(int i=1; i<=m1; i++)
    {
        int x,y,z;
        scanf("%d%d%d", &x, &y, &z);
        v[x].emplace_back(y, z);
        v[y].emplace_back(x, z);
        fa[find(x)]=find(y);
    }
    for(int i=1; i<=m2; i++)
    {
        int x,y,z;
        scanf("%d%d%d", &x, &y, &z);
        vv[x].emplace_back(y, z);
        b[find(y)].push_back(y); ++rd[find(y)];
        c[find(x)].push_back(x);
    }

    memset(dis, 0x3f, sizeof(dis));
    b[find(s)].push_back(s); dis[s]=0;
    for(int i=1; i<=n; i++)
    if(find(i)==i&&!rd[i])
    q.push(i);
    
    while(q.size())
    {
        int x=q.front(); q.pop();
        dij(x);
        for(int y : c[x])
        for(pair<int,int> e : vv[y])
        {
            dis[e.first]=min(dis[e.first], dis[y]+e.second);
            int fy=find(e.first);
            if(!--rd[fy])
            q.push(fy);
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(dis[i]>1e15) puts("NO PATH");
        else printf("%lld\n", dis[i]);
    }
    return 0;
}
2023/3/18 16:32
加载中...