AcWing 1129. 热浪
原题链接
简单
作者:
abc_9
,
2020-08-29 19:55:18
,
所有人可见
,
阅读 393
import java.util.*;
class Pair{
int a;
int b;
public Pair(int a, int b){
this.a = a;
this.b = b;
}
}
public class Main{
public static final int N = 6210 * 2 , max = 0x3f3f3f3f;
public static int[] e = new int[N];
public static int[] ne = new int[N];
public static int[] h = new int[N];
public static int[] w = new int[N];
public static int[] dist = new int[N];
public static boolean[] feasible = new boolean[N];
public static int n , m , s , r , idx = 0;
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 int dijkstra(){
PriorityQueue<Pair> q = new PriorityQueue<>((p1 , p2) -> {return p1.a - p2.a;});
Arrays.fill(dist , max);
dist[s] = 0;
q.offer(new Pair(0 , s));
while(q.size() != 0)
{
Pair t = q.poll();
int distance = t.a;
int num = t.b;
if(feasible[num]) continue;
feasible[num] = true;
for(int i = h[num] ; i != -1 ; i = ne[i])
{
int j = e[i];
if(dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
q.offer(new Pair(dist[j] , j));
}
}
}
return dist[r];
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
s = sc.nextInt();
r = sc.nextInt();
Arrays.fill(h , -1);
while(m -- > 0)
{
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
// 是双向道路,记得加入a->b b-> 两条边
add(a , b , c);
add(b , a , c);
}
int t = dijkstra();
System.out.println(t);
}
}