P3371 【模板】单源最短路径(弱化版)(java实现)

P3371 【模板】单源最短路径(弱化版)

题目描述

题目背景

本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779

题目描述

如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度。

输入格式

第一行包含三个整数 n,m,s,分别表示点的个数、有向边的个数、出发点的编号。

接下来 m 行每行包含三个整数 u,v,w,表示一条uv 的,长度为 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]+" ");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜柠函

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值