AcWing 853. 有边数限制的最短路:JAVA简单写法
原题链接
简单
作者:
ARM
,
2020-08-11 19:10:25
,
所有人可见
,
阅读 597
java 代码
import java.io.*;
import java.lang.*;
import java.util.*;
class Main{
static int n = 0, m = 0, k = 0;
static int N = 10010;
static int[][] edges = new int[N][3];
static int[] dist = new int[510];
static int[] backup = new int[510];
static int bellmanFord(){
Arrays.fill(dist, 0x3f3f3f3f);
dist[1] = 0;
for(int i = 0; i < k; ++i){
backup = Arrays.copyOf(dist, N);
for(int j = 1; j <= m; ++j){
int a = edges[j][0], b = edges[j][1], w = edges[j][2];
dist[b] = Math.min(dist[b], backup[a] + w);
}
}
if(dist[n] >= 0x3f3f3f3f / 2)return -1;
return dist[n];
}
public static void main(String[] args)throws Exception{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] params = buf.readLine().split(" ");
n = Integer.valueOf(params[0]);
m = Integer.valueOf(params[1]);
k = Integer.valueOf(params[2]);
for(int i = 1; i <= m; ++i){
String[] info = buf.readLine().split(" ");
edges[i][0] = Integer.valueOf(info[0]);//入
edges[i][1] = Integer.valueOf(info[1]);//出
edges[i][2] = Integer.valueOf(info[2]);//权值
}
int cond = bellmanFord();
if(cond != -1){
System.out.print(cond);
}else{
System.out.print("impossible");
}
}
}
可以,可以,用数组来存