PAT1003

//代码没有100%通过

package PAT1003;

import java.util.Scanner;

public class Main {
    static int[][] edges = new int[500][500];
    static int[] vexs = new int[500];
    static boolean[] visit = new boolean[500];
    static int[] destince = new int[500];
    static int[] preNode = new int[500];

    public static void main(String[] args) {
        int n, m, c1, c2;
        int numPeople;
        int startCity, endCity, valueEdge;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        c1 = sc.nextInt();
        c2 = sc.nextInt();

        if(c1 == c2) {
            System.out.println(0 + " " + vexs[c1]);
            return;
        }

        for(int i = 0; i < n; i++) {
            vexs[i] = sc.nextInt();
            visit[i] = false;
        }

        for(int i = 0; i < m; i++) {
            startCity = sc.nextInt();
            endCity = sc.nextInt();
            valueEdge = sc.nextInt();
            edges[startCity][endCity] = valueEdge;
            edges[endCity][startCity] = valueEdge;
        }


        for(int i = 0; i < n; i++) {
            destince[i] = edges[c1][i];
        }


        visit[c1] = true;
        numPeople = vexs[c1];


        preNode[c1] = c1;


        int u = c1;
        for(int i = 0; i < n; i++) {
            int minDistance = Integer.MAX_VALUE;
            int maxPeople = Integer.MIN_VALUE;
            for(int j = 0; j < n; j++) {
                if (visit[j] == false && destince[j] <= minDistance && destince[j] != 0) {
                    if (vexs[j] >= maxPeople) {
                        minDistance = destince[j];
                        maxPeople = vexs[j];
                        u = j;
                    }
                }
            }

            visit[u] = true;

            if(visit[c2] == true) {
                break;
            }

            for(int j = 0; j < n; j++) {
                int tmp;
                if (visit[j] == false) {
                    if (edges[u][j] != 0) {
                        tmp = destince[u] + edges[u][j];
                        if (destince[j] == 0 || destince[j] > tmp) {
                            destince[j] = tmp;
                            preNode[j] = u;
                        } else if (destince[j] == tmp) {
                            destince[j] = tmp;
                            preNode[j] = u;
                        }
                    }
                }
            }
        }
        for(int i = c2; i != c1; i = preNode[i]) {
            numPeople += vexs[i];
        }
        System.out.println(destince[c2] +  " " + numPeople);
        /*
        for(int row = 0; row < n; row++) {
            for(int col = 0; col < n; col++) {
                System.out.print(edges[row][col]+ " ");
            }
            System.out.println();
        }
        */
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值