AcWing 853. 有边数限制的最短路
原题链接
简单
作者:
鸡娃练习生
,
2024-11-18 16:54:34
,
所有人可见
,
阅读 2
import java.util.*;
public class Main{
static int N = 510,M = 100010;
static int n,m,k;
static int[] dist = new int[N],back = new int[N];
static Node[] node = new Node[M];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
Arrays.fill(dist,0x3f3f3f3f);
for(int i = 0;i < m;i++){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
node[i] = new Node(a,b,c);
}
bellman_ford();
}
static void bellman_ford(){
dist[1] = 0;
for(int i = 0;i < k;i++){
back = Arrays.copyOf(dist,n + 1);
for(int j = 0;j < m;j++){
Node no = node[j];
int a = no.a;
int b = no.b;
int c = no.c;
dist[b] = Math.min(back[a] + c,dist[b]);
}
}
if(dist[n] > 0x3f3f3f3f/2){
System.out.print("impossible");
}else{
System.out.print(dist[n]);
}
}
static class Node{
int a,b,c;
public Node(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
}
}