POJ(Java) - 2387 Til the Cows Come Home

POJ(Java) - 2387 Til the Cows Come Home

上一篇是关于树的遍历,感兴趣可以戳这里树的遍历及前中后互求


一、前言

这篇关于Dijkstra算法。poj的这道题是一道很基础的Dijkstra算法,照着固定的代码即可求出正确答案。虽然代码简单,但我还是以为你应该明白以下几点:

  1. Dijkstra算法为什么不能处理负权值问题?
  2. 如果有负权值应该怎么处理?
    如果能清楚理解以上的两个问题,那么对于该算法就有一个大致清楚的了解,就能在做题时候规避一些不必要的麻烦。

二、题目

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

三、代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Dijkstra {
    static int N;//点个数
    static int T;//边个数
    static ArrayList<int[]>[] list;
    static int[] ans;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        N = sc.nextInt();
        list = new ArrayList[N+1];
        ans = new int[N+1];
        for (int i = 1; i <= N; i++) {
            list[i] = new ArrayList();
            ans[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < T; i++) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int l = sc.nextInt();
            list[m].add(new int[]{n,l});
            list[n].add(new int[]{m,l});
        }
        ans[1] = 0;
        handle(1);
        System.out.println(ans[N]);
    }

    private static void handle(int start) {
//        PriorityQueue<int[]> pq = new PriorityQueue ((int[] i1,int[] i2) -> i1[1]-i2[1]);
        PriorityQueue<int[]> pq = new PriorityQueue(N,new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        pq.add(new int[]{start,0});
        while (!pq.isEmpty()){
            int[] cur = pq.poll();
            // 结束条件
            if(cur[0] == N)return;
            if(list[cur[0]].size() > 0){
                for (int [] next : list[cur[0]]) {
                    int [] tmp = next.clone();
                    tmp[1] += cur[1];
                    // 路径压缩
                    if(tmp[1] < ans[tmp[0]]){
                        ans[tmp[0]] = tmp[1];
                        pq.add(tmp);
                    }
                }
            }
        }
    }
}

四、总结

以上就是代码示例,注意的两点:

  • 关于结束条件,这对图是优化的关键所在;
  • 路径压缩,点的判断就是围绕路径压缩进行。另外,路径压缩对于好多算法都会用到,还是细细体会其中的奥妙。

五、结束语

最后,正在算法坑中痛苦前行的小白一枚,有错的地方欢迎指正探讨!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值