728x90
반응형
1. 문제
10282번: 해킹
최흉최악의 해커 yum3이 네트워크 시설의 한 컴퓨터를 해킹했다! 이제 서로에 의존하는 컴퓨터들은 점차 하나둘 전염되기 시작한다. 어떤 컴퓨터 a가 다른 컴퓨터 b에 의존한다면, b가 감염되면
www.acmicpc.net
2. 풀이
- 다익스트라 응용
3. 코드
import java.util.*;
import java.io.*;
public class Main {
public static class Node implements Comparable<Node> {
int to, weight;
public Node(int to, int weight) {
this.to = to;
this.weight = weight;
}
public int compareTo(Node o) {
return this.weight - o.weight;
}
}
static final int INF =987654321;
static int v,e,c;
static int[]visited;
static ArrayList<Node>[]list;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int tc = Integer.parseInt(br.readLine());
for (int i = 0; i < tc; i++) {
st = new StringTokenizer(br.readLine());
v = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
int start = Integer.parseInt(st.nextToken());
list = new ArrayList[v + 1];
visited = new int[v + 1];
Arrays.fill(visited, INF);
c = 1;
for (int j = 1; j <= v; j++)
list[j] = new ArrayList<>();
for (int j = 0; j < e; j++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
list[to].add(new Node(from, cost));
}
bfs(start);
int[] result = Arrays.stream(visited).filter(k -> k != INF).toArray();
Arrays.sort(result);
System.out.println(c+" "+result[result.length - 1]);
}
}
public static void bfs(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
visited[start] = 0;
pq.offer(new Node(start, visited[start]));
while (!pq.isEmpty()) {
Node from = pq.poll();
if (from.weight > visited[from.to])
continue;
for (Node to : list[from.to]) {
if (visited[to.to] > visited[from.to] + to.weight) {
if(visited[to.to] == INF)
c++;
visited[to.to] = visited[from.to] + to.weight;
pq.offer(new Node(to.to, visited[to.to]));
}
}
}
}
}
반응형
반응형
'알고리즘 > 그래프' 카테고리의 다른 글
[백준 1981] 배열에서 이동(Java) (0) | 2020.12.30 |
---|---|
[백준 1753] 최단경로(Java) (0) | 2020.12.29 |
[백준 6087] 레이저 통신(Java) (0) | 2020.12.28 |
[백준 1963] 소수 경로(Java) (0) | 2020.12.23 |
[백준 1504] 특정한 최단 경로(Java) (0) | 2020.12.22 |