P3371 【模板】单源最短路径(弱化版)
题目描述
题目背景
本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779。
题目描述
如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度。
输入格式
第一行包含三个整数 n,m,s,分别表示点的个数、有向边的个数、出发点的编号。
接下来 m 行每行包含三个整数 u,v,w,表示一条u→v 的,长度为 w的边。
输出格式
输出一行 n 个整数,第 ii 个表示 s到第 i 个点的最短路径,若不能到达则输出 2^{31}-1。
输入输出样例
输入 #1
4 6 1
1 2 2
2 3 2
2 4 1
1 3 5
3 4 3
1 4 4
输出 #1
0 2 4 3
题解一
package p3371;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
// 临接矩阵过不了
public class Main {
static int n,m,s;
static int N = 10010,INF = 0x3f3f3f3f;
static int g[][] = new int[N][N];
static int dist[] = new int[N];
static boolean st[] = new boolean[N];
public static void spfa() {
Queue<Integer> q = new LinkedList<>();
q.add(s);
Arrays.fill(dist, INF);
Arrays.fill(st, false);
dist[s] = 0;
st[s] = true;
while(!q.isEmpty()) {
int t = q.poll();
for(int i=1;i<=n;i++) {
if(g[t][i]==INF) continue;
int j = g[t][i];
if(dist[i]>dist[t]+j) {
dist[i] = dist[t]+j;
if(!st[i]) {
q.add(i);
st[i] = true;
}
}
}
}
}
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str[] = bf.readLine().split(" ");
n = Integer.parseInt(str[0]);m = Integer.parseInt(str[1]);s = Integer.parseInt(str[2]);
for(int i=1;i<=n;i++) {
for(int j=0;j<=n;j++) {
g[i][j] = INF;
}
}
while(m-->0) {
str = bf.readLine().split(" ");
int a = Integer.parseInt(str[0]),b = Integer.parseInt(str[1]),c = Integer.parseInt(str[2]);
g[a][b]=Math.min(g[a][b], c);
}
spfa();
for(int i=1;i<=n;i++) {
if(dist[i]==INF) {
System.out.println(Integer.MAX_VALUE+" ");
}
System.out.print(dist[i]+" ");
}
}
}
题解二
package p3371;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
// 临接表
public class Main2 {
static int m,n,s;
static int N = 10010, M = 500010,INF = 0x3f3f3f3f;
static int h[] = new int[N],e[] = new int[M],ne[] = new int[M],w[] = new int[M],idx = 0;
static int dist[] = new int[N];
static boolean st[] = new boolean[N];
public static void add(int a,int b,int c){
e[idx] = b;ne[idx] = h[a];w[idx] = c;h[a] = idx++;
}
public static void spfa() {
Queue<Integer> q = new LinkedList<>();
q.add(s);
Arrays.fill(dist,INF);
Arrays.fill(st, false);
dist[s] = 0;
st[s] = true;
while(!q.isEmpty()) {
int t = q.poll();
st[t] = false;
for(int i=h[t];i!=-1;i = ne[i]) {
int j = e[i];
if(dist[j]>dist[t]+w[i]) {
dist[j] = dist[t]+w[i];
if(!st[j]) {
q.add(j);
st[j] = true;
}
}
}
}
}
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] str = bf.readLine().split(" ");
n = Integer.parseInt(str[0]);m = Integer.parseInt(str[1]);s = Integer.parseInt(str[2]);
Arrays.fill(h, -1);
while(m-->0){
str = bf.readLine().split(" ");
int a = Integer.parseInt(str[0]),b = Integer.parseInt(str[1]),c = Integer.parseInt(str[2]);
add(a,b,c);
}
bf.close();
spfa();
for(int i=1;i<=n;i++) {
if(dist[i]==INF) {
System.out.print(Integer.MAX_VALUE+" ");
continue;
}
System.out.print(dist[i]+" ");
}
}
}