import java.util.*;
public class Main {
//使用邻接表存图
static class edge{
int to;
int val;
public edge(int to, int val) {
this.to = to;
this.val = val;
}
}
static List<edge>[] list;
static int[] dis;
static int INF = 0x3f3f3f3f;
static int T,R,P,S;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
R = sc.nextInt();
P = sc.nextInt();
S = sc.nextInt();
list = new ArrayList[T];
//设置标记,为true则存无向图,false存有向图
boolean flag = false;
for(int i = 0;i < R+P;i++){
int s = 0;
int y = 0;
if(i < R){
flag = true;
}else{
flag = false;
}
for(int j = 0;j<3;j++){
int p = sc.nextInt();
if (j == 0) {
s = p;
} else if(j == 1){
y = p;
}else {
if(list[s-1] == null){
list[s-1] = new ArrayList<>();
}
if(list[y-1] == null){
list[y-1] = new ArrayList<>();
}
list[s-1].add(new edge(y-1,p));
if(flag){
list[y-1].add(new edge(s-1,p));
}
}
}
}
spfa(S);
for(int i = 0;i<T;i++){
if(i == S-1){
System.out.println(0);
}else if(dis[i] == INF){
System.out.println("NO PATH");
}else {
System.out.println(dis[i]);
}
}
}
//SPFA算法核心代码
public static void spfa(int S){
dis = new int[T];
Arrays.fill(dis,INF);
dis[S-1] = 0;
Queue<Integer> q = new ArrayDeque<>();
q.add(S-1);
while(!q.isEmpty()){
int a = q.poll();
for(edge e : list[a]){
if(dis[a] + e.val < dis[e.to]){
dis[e.to] = dis[a] + e.val;
q.add(e.to);
}
}
}
}
}
只跑了75分,不知如何优化,有跑满的大佬请在评论区分享您的代码(java)