吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1240|回复: 0
收起左侧

[Java 转载] 【笔记】数据结构与算法之最短路算法(Bellman_Ford)

  [复制链接]
QingYi. 发表于 2021-1-2 09:55
适用于存在负权边的情况下
[Java] 纯文本查看 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
    static class Edge{
        int a,b,w;
        Edge(int a,int b ,int w){
            this.a = a;
            this.b = b;
            this.w = w;
        }
    }
    static int M = 100010;
    private static int N= 510,INF= 0x3f3f3f3f;
    static  Edge [] edges = new Edge[M];
    static int[] dist = new int[N];
    static int[] backup = new int[N];
    static int n,m,k;
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        String[] s = bf.readLine().split(" ");
//        总共n个点m条边且顶多经过k条边
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        k = Integer.parseInt(s[2]);
        for (int i = 0; i < m; i++) {
            String []  re = bf.readLine().split(" ");
            //分别是第一条边和第二条边
            int a = Integer.parseInt(re[0]);
            int b = Integer.parseInt(re[1]);
            //距离
            int w = Integer.parseInt(re[2]);
            edges[i] = new Edge(a,b,w);
        }
        int res = bellmanFord();
        //找不到最短路,例如有负权边进入死循环导致正无穷大
        if (res==-1){
            System.out.println("impossible");
        }else {
            System.out.println(res);
        }
    }




    private static int bellmanFord() {
        //初始化操作
        Arrays.fill(dist,INF);
        dist[1] = 0;
        //循环顶多进行k次
        for (int i = 0; i < k; i++) {
            backup = dist.clone();
            for (int j = 0; j < m; j++) {
                int a = edges[j].a;
                int b = edges[j].b;
                int w = edges[j].w;
                //使用备份去避免串联,用无穷大来更新,取得最小值
                dist[b] = Math.min(dist[b],backup[a]+w);
            }
        }
        //因为有最大值INF,还有负权,加上负权边可能不是INF了,可能比INF小,所以判断大于一半就行
        if (dist[n]>INF/2){
            return -1;
        }
        return dist[n];
    }
}

在新标签打开所有链接复制所有链接URL复制所有链接URL(反向)复制所有链接标题 + URL复制所有链接标题 + URL (MD)复制所有链接标题 + URL (BBS)复制所有链接标题 + URL (筛选)复制所有链接标题 + URL (设置复制格式)在新标签页打开所有图片链接在一个标签页显示所有图片链接
复选框 - 选中
复选框 - 取消
复选框 - 反选
单选框 - 选中
单选框 - 取消
特殊单选框 - 选中

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 23:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表