https://www.acmicpc.net/problem/2211
MST로 구하면 될줄 알았는데...
다름 조건 때문에 단순 MST로는 구할수 없다.
|
다익스트라를 변형해서, 1번부터 K번까지의 최단거리를 구하는 문제로 변경함.
다익스트라 변경은 머리에 잘 안들어옮. ㅠㅠ
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class BOJ2211 {
static int N, M;
static ArrayList<Edge>[] edges;
static int[] D;
static class Edge implements Comparable<Edge> {
int s,e, v;
Edge(int s, int e, int v){
this.s = s;
this.e = e;
this.v = v;
}
@Override
public int compareTo(Edge o) {
return this.v - o.v;
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("res/input_boj2211.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine().trim(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
edges = new ArrayList[N+1];
D = new int[N+1];
for (int i = 1; i <=N ; i++) edges[i] = new ArrayList<>();
int a, b, c;
for (int i = 0; i < M ; i++) {
st = new StringTokenizer(br.readLine().trim(), " ");
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
edges[a].add(new Edge(a, b,c));
edges[b].add(new Edge(b, a,c));
}
Arrays.fill(D, Integer.MAX_VALUE);
PriorityQueue<Edge> pq = new PriorityQueue<>();
Edge ed = new Edge(0,1,0);
pq.add(ed);
ArrayList<Edge> res = new ArrayList<>();
while (!pq.isEmpty()){
ed = pq.poll();
if(D[ed.e]!=Integer.MAX_VALUE) continue;
D[ed.e] = ed.v;
if(ed.s != 0) {
res.add(ed);
}
for (Edge nd:edges[ed.e]) {
if(D[nd.e]!=Integer.MAX_VALUE) continue;
//D[nd.e] = D[ed.e] + nd.v;
pq.add(new Edge(ed.e, nd.e, ed.v+nd.v));
}
}
System.out.println(res.size());
for (Edge r: res) {
System.out.println(r.s+" "+r.e);
}
}
}