编程语言
首页 > 编程语言> > Java P1807 最长路

Java P1807 最长路

作者:互联网

题目链接
bfs,图论最长,不是以往的最短

**import java.util.*;
import java.math.*;
public class Main {
	public static int n,m,res=0;
	public static int[] price;
	public static int[][] quan;
    public static void main(String[] args){
    	Scanner sc=new Scanner(System.in);
    	n=sc.nextInt();
    	quan=new int[n+1][n+1];
    	price=new int[n+1];
    	Arrays.fill(price,-1);
    	price[1]=0;
    	m=sc.nextInt();
    	for(int i=0;i<m;i++){
    		int a=sc.nextInt();
    		int b=sc.nextInt();
    		int c=sc.nextInt();
    		quan[a][b]=Math.max(quan[a][b],c);
    	}
    	bfs();
    	System.out.println(price[n]);
    }
    public static void bfs(){
    	Queue<Integer> q=new LinkedList<Integer>();
    	q.offer(1);
    	while(!q.isEmpty()){
    		int cur=q.poll();
    		for(int i=1;i<=n;i++){
    			if(quan[cur][i]!=0&&price[i]<price[cur]+quan[cur][i]){
    				price[i]=price[cur]+quan[cur][i];
    				q.offer(i);
    			}
    		}
    	}
    }
    
}

**

标签:Java,nextInt,int,price,public,P1807,quan,sc,最长
来源: https://blog.csdn.net/weixin_45665566/article/details/113916279