zoukankan      html  css  js  c++  java
  • 最短路 bellman-ford算法

    问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环)。请你计算从1号点到其他点的最短路(顶点从1到n编号)。 输入格式 第一行两个整数n, m。 接下来的m行,每行有三个整数u, v, l,表示u到v有一条长度为l的边。 输出格式 共n-1行,第i行表示1号点到i+1号点的最短路。 样例输入 3 3 1 2 -1 2 3 -1 3 1 2 样例输出 -1 -2 数据规模与约定 对于10%的数据,n = 2,m = 2。 对于30%的数据,n <= 5,m <= 10。 对于100%的数据,1 <= n <= 20000,1 <= m <= 200000,-10000 <= l <= 10000,保证从任意顶点都能到达其他所有顶点。

     1 package job525;
     2 
     3 import java.util.Scanner;
     4 
     5 class Eage{
     6     int from;
     7     int to;
     8     int cost;
     9 
    10     public int getFrom() {
    11         return from;
    12     }
    13     public void setFrom(int from) {
    14         this.from = from;
    15     }
    16     public int getTo() {
    17         return to;
    18     }
    19     public void setTo(int to) {
    20         this.to = to;
    21     }
    22     public int getCost() {
    23         return cost;
    24     }
    25     public void setCost(int cost) {
    26         this.cost = cost;
    27     }
    28     public Eage(int from,int to,int cost)
    29     {
    30         this.from = from;
    31         this.to = to;
    32         this.cost = cost;
    33     }
    34 }
    35 
    36 public class 最短路bellman_ford {
    37     
    38     static int m,n,inf=100000;//n:n个顶点;m:m条边
    39     static int []d=new int[20000];
    40     static Eage []eg=new Eage[200000];
    41     
    42     public static void main(String[] args) {
    43         Scanner sc=new Scanner(System.in);
    44         n=sc.nextInt();m=sc.nextInt();
    45         int i=0;
    46         while(i<m)
    47         {
    48             int u, v, l;
    49             u=sc.nextInt();
    50             v=sc.nextInt();
    51             l=sc.nextInt();
    52             eg[i++]=new Eage(u,v,l);
    53         }
    54         //测试存储---------------------------------------------------------------------------------
    55         /*
    56         for(i=0;i<m;i++)
    57         {
    58             System.out.println("("+eg[i].getFrom()+","+eg[i].getTo()+"):"+eg[i].getCost());
    59         }
    60         */
    61         //测试结束----------------------------------------------------------------------------------
    62         bellman_ford();
    63         for(i=2;i<=n;i++)
    64         {
    65             System.out.println(d[i]);
    66         }
    67     }
    68     public static void bellman_ford()
    69     {
    70         init();
    71         while(true)
    72         {
    73             boolean flag=false;
    74             for(int i=0;i<m;i++)
    75             {
    76                 Eage e=eg[i];
    77                 if(d[e.getFrom()]!=inf&&d[e.getTo()]>d[e.getFrom()]+e.getCost())
    78                 {
    79                     d[e.getTo()]=d[e.getFrom()]+e.getCost();
    80                     //System.out.println("("+eg[i].getFrom()+","+eg[i].getTo()+"):"+eg[i].getCost()+d[e.getTo()]);//测试
    81                     flag=true;
    82                 }
    83             }
    84             if(!flag)
    85                 break;
    86         }
    87     }
    88     public static void init()
    89     {
    90         for(int i=1;i<=n;i++)
    91             d[i]=inf;
    92         d[1]=0;
    93     }
    94 }
    View Code

      

  • 相关阅读:
    leetcode 131. Palindrome Partitioning
    leetcode 526. Beautiful Arrangement
    poj 1852 Ants
    leetcode 1219. Path with Maximum Gold
    leetcode 66. Plus One
    leetcode 43. Multiply Strings
    pytorch中torch.narrow()函数
    pytorch中的torch.repeat()函数与numpy.tile()
    leetcode 1051. Height Checker
    leetcode 561. Array Partition I
  • 原文地址:https://www.cnblogs.com/dashen/p/3755025.html
Copyright © 2011-2022 走看看