P4779 【模板】单源最短路径(标准版)
题目描述
给定一个 n个点,m条有向边的带非负权图,请你计算从 s出发,到每个点的距离。
数据保证你能从 s出发到任意点。
输入格式
第一行为三个正整数 n, m, s。 第二行起 m行,每行三个非负整数 u, v, w,表示从 u到 v有一条权值为 w的有向边。
输出格式
输出一行 n个空格分隔的非负整数,表示 s到每个点的距离。
输入输出样例
输入 #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 p4779;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
// spfa
public class Main {
static int m,n,s;
static int N = 100010, M = 200010,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++) {
System.out.print(dist[i]+" ");
}
}
}
题解二
package p4779;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
// 堆排序的dijkstra
public class Main2 {
static int n,m,s;
static int N = 100010,M = 200010,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];
static void add(int a,int b, int c) {
e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx++;
}
static void dijkstra() {
PriorityQueue<Pair> q = new PriorityQueue<>();
Arrays.fill(dist, INF);
Arrays.fill(st, false);
q.add(new Pair(s,0));
dist[s] = 0;
while(!q.isEmpty()) {
Pair t = q.poll();
int ver = t.x;
if(st[ver]) continue;
st[ver] = true;
for(int i=h[ver];i!=-1;i=ne[i]) {
int j = e[i];
if(dist[j]> dist[ver]+w[i]) {
dist[j] = dist[ver] + w[i];
q.add(new Pair(j,dist[j]));
}
}
}
}
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();
dijkstra();
for(int i=1;i<=n;i++) {
System.out.print(dist[i]+" ");
}
}
}
class Pair implements Comparable<Pair>{
int x,y;
public Pair(int x,int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return this.y-o.y;
}
}