java的MLE求助!!!!!!!!!!!!!!!

P4779 【模板】单源最短路径(标准版)

CY000 @ 2023-03-28 20:44:06

真的不懂JAVA,求大佬看看怎么MLE的吧,给您磕头了qwq

package com.company;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main
{
    static class edge {
        int dis,next,to;
    }
    static class node {
        node(int dis,int pos) {
            this.dis=dis;
            this.pos=pos;
        }
        int dis,pos;
    }
    static class nodecmp implements Comparator<node> {
        @Override
        public int compare(node a,node b) {
            return a.dis-b.dis;
        }
    }
    public static int dis[];
    public static edge e[];
    public static int head[];
    public static int tot,n,m,s;
    public static boolean vis[];
    public static void add(int from,int to,int dis)
    {
        e[++tot] = new edge();
        e[tot].dis=dis;e[tot].to=to;e[tot].next=head[from];
        head[from]=tot;
    }
    public static void dij()
    {
        PriorityQueue<node> q=new PriorityQueue<>(new nodecmp());
        dis[s]=0;
        q.add(new node(0,s));
        while(!q.isEmpty())
        {
            node temp=q.peek();
            q.remove();
            int now=temp.pos;
            if(vis[now])
                continue;
            vis[now]=true;
            for(int i=head[now];i!=0;i=e[i].next)
            {
                int to=e[i].to;
                if(dis[to]>dis[now]+e[i].dis)
                {
                    dis[to]=dis[now]+e[i].dis;
                    if(!vis[to])
                        q.add(new node(dis[to],to));
                }
            }
        }
    }
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        n=cin.nextInt();m=cin.nextInt();s=cin.nextInt();
        dis = new int[n+10];head = new int[n+10];vis = new boolean[n+10];
        e = new edge[2*m+10];
        for(int i=1;i<=n;i++)
            dis[i]=1000000000;
        for(int i=1;i<=m;i++)
            add(cin.nextInt(),cin.nextInt(),cin.nextInt());
        dij();
        for(int i=1;i<=n;i++)
            System.out.print(dis[i]+" ");
        cin.close();
    }
}

|