样例过了,和题解对拍也没问题,但是WA了,求调

UVA1599 Ideal Path

lijunxi2026 @ 2024-11-14 20:38:53

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>

using namespace std;

const int N=400010;

int n,m;

struct Node{
    int to,nxt;
    int w;
}edge[N<<1];

int head[N],cnt=0;

void add(int u,int v,int w){
    edge[++cnt].to=v,edge[cnt].nxt=head[u],edge[cnt].w=w,head[u]=cnt;
}

int dis[N];

void bfs(){
    memset(dis,-1,sizeof(dis));
//  cout<<dis[0]<<endl;
    queue<int>q;
    q.push(n);
    dis[n]=0;
    while(!q.empty()){
        auto t=q.front();
        q.pop();
        for(int i=head[t];i;i=edge[i].nxt){
            int v=edge[i].to;
            if(dis[v]!=-1)continue;
            dis[v]=dis[t]+1;
            q.push(v);
        }
    }

//  for(int i=1;i<=n;i++){
//      cout<<dis[i]<<" ";
//  }
//  cout<<endl;
}

bool vis[N];

queue<int>g;

void get(){
    queue<int>q;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        vector<int>t;
        while(!q.empty()){
            t.push_back(q.front());
            q.pop();
        }
        int minn=0x3f3f3f3f;
        for(int j=0;j<t.size();j++){
            int u=t[j];
            for(int i=head[u];i;i=edge[i].nxt){
                int v=edge[i].to;
                if(dis[v]+1==dis[u]){
//                  cout<<minn<<endl;
                    minn=min(minn,edge[i].w);
                }
            }
        }
//      cout<<minn<<endl;
        if(minn==0x3f3f3f3f)return;
        g.push(minn);
        for(int j=0;j<t.size();j++){
            int u=t[j];
            for(int i=head[u];i;i=edge[i].nxt){
                int v=edge[i].to;
                if(dis[v]+1==dis[u]&&(edge[i].w==minn)&&!vis[v]){
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }

//  for(int i=1;i<=n;i++){
//      cout<<col[i]<<" ";
//  }
//  cout<<endl;

}

void work(){
    memset(vis,0,sizeof(vis));
    memset(head,0,sizeof(head));
    cnt=0;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        add(u,v,w),add(v,u,w);
    }
    bfs();
    get();
    cout<<g.size()<<endl;
    while(g.size()){
        cout<<g.front();
        if(g.size()==1)cout<<endl;
        else cout<<" ";
        g.pop();
    }
}

signed main(){
    freopen("in.txt","r",stdin);
    freopen("in.out","w",stdout);
    while(scanf("%lld%lld",&n,&m)!=-1){
        work();
    }
    return 0;
}

样例过了,和题解对拍也没问题,但是WA了,求调


|