WA求救,不知道自己错哪了
查看原帖
WA求救,不知道自己错哪了
921737
steins040617楼主2023/10/10 13:12
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<string.h>
using namespace std;
struct Node
{
    int v;
    long long dis;
    bool operator <(const Node & t)const
    {
        return dis<t.dis;
    }
};
int nv,ne;
long long dis[100005];
vector <int> v[100005];
vector <int> w[100005];
priority_queue <struct Node> q;
bool col[100005];
int path[100005];
void Dijkstra();
void Print();
int main()
{
    int wei,i,v1,v2;
    scanf("%d%d",&nv,&ne);
    for(i=1;i<=ne;i++)
    {
        scanf("%d%d%d",&v1,&v2,&wei);
        v[v1].push_back(v2);
        v[v2].push_back(v1);
        w[v1].push_back(wei);
        w[v2].push_back(wei);
    }//初始化
    memset(path,-1,sizeof(path));
    Dijkstra();
	if(path[nv]==-1)
		printf("-1");
	else
	Print();
    return 0;
}
void Print()
{
    stack <int> s;
    int i=nv,ans;
	s.push(nv);
    while(path[i]!=-1)
    {
        s.push(path[i]);
        i=path[i];
    }
    while(!s.empty())
    {
        ans=s.top();
        s.pop();
        printf("%d ",ans);
    }
    return ;
}
void Dijkstra()
{
    int i,ve;
    struct Node tn,t;
    for(i=1;i<=nv;i++)
    {
        dis[i]=0x7fffffff;
        col[i]=false;
    }
    dis[1]=0;
    tn.v=1,tn.dis=0;
    q.push(tn);
    while(!q.empty())
    {
        t=q.top();
        q.pop();
		if(col[t.v]) continue;
		col[t.v]=true;
        ve=t.v;
        for(i=0;i<v[ve].size();i++)
        {
            int v2=v[ve][i];//连接着的节点w
            if(t.dis+w[ve][i]<dis[v2])
            {
                dis[v2]=t.dis+w[ve][i];
                path[v2]=ve;
                t.v=v[ve][i];
                t.dis=dis[v2];
                q.push(t);
            }
        }
    }
}
2023/10/10 13:12
加载中...