敢问MLE怎么整
查看原帖
敢问MLE怎么整
121122
木守球楼主2021/7/23 20:18
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
    static class Node{
        int to,weight,next;
        public Node(int to, int weight, int next) {
            this.to = to;
            this.weight = weight;
            this.next = next;
        }
    }
    static class ComNode implements Comparable<ComNode>{
        Integer to;
        Integer weight;

        public ComNode(Integer to, Integer weight) {
            this.to = to;
            this.weight = weight;
        }
        @Override
        public int compareTo(ComNode o) {
            return weight.compareTo(o.weight);
        }
    }
    static int[] head;
    static Node[] edge;
    static void dijkstra(int start,Integer[] dis,int cap){
        PriorityQueue<ComNode> heap = new PriorityQueue<ComNode>(cap);
        heap.add(new ComNode(start,0));
        for(int j=head[start];j!=0;j=edge[j].next){
            ComNode temp = new ComNode(edge[j].to,edge[j].weight);
            heap.add(temp);
        }
        for(int i=1;i<dis.length;i++){
            ComNode next=heap.poll();
            while(dis[next.to]!=null)next = heap.poll();
            dis[next.to]=next.weight;
            for(int j=head[next.to];j!=0;j=edge[j].next){
                ComNode temp = new ComNode(edge[j].to,edge[j].weight+dis[next.to]);
                heap.add(temp);
            }
        }
    }
    public static void main(String[] args)throws Exception{
        int m,n,s,j=1;//m个节点 n个边 s起点
        Scanner scanner = new Scanner(System.in);
        m=scanner.nextInt();
        n=scanner.nextInt();
        s=scanner.nextInt();
        head = new int[m+1];
        edge = new Node[n+1];
        Integer[] dis = new Integer[m+1];
        for(int i=1;i<=n;i++){// 经测试,循环后,1e5的数据量就达到120mb左右
            int from=scanner.nextInt(),to=scanner.nextInt(),weight=scanner.nextInt();
            Node node = new Node(to,weight,head[from]);
            edge[j]=node;
            head[from]=j++;
        }
        dijkstra(s,dis,n);
        for(int i=1;i<=m;i++)
            System.out.print(dis[i]+" ");
    }
}

请不要扯到语言劣根性的问题上,我见过最高贵的java,也见过最低劣的C++;

2021/7/23 20:18
加载中...