티스토리 뷰
http://www.koitp.org/problem/SDS_PRO_10_4/read/
| 시간 제한 | 메모리 제한 | 제출 횟수 | 정답 횟수 (비율) | 정답자 수 |
|---|---|---|---|---|
| 2.0 초 | 512 MB | 710 | 202 (28%) | 149 |
>> prim kruskal 알고리즘
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
/*
예제 입력
5
8
1 2 4
1 3 9
1 4 21
2 3 8
2 4 17
3 4 16
5 2 20
5 4 30
예제 출력
48
*/
class Doro{
int node, cost;
public Doro(int node1, int cost) {
super();
this.node = node1;
this.cost = cost;
}
}
class source{
public static void main(String[] args)
throws IOException
{
InputStreamReader fr = new InputStreamReader(System.in);
// FileReader fr = new FileReader("D:\\dev\\worksspace\\Solution\\src\\source");
BufferedReader br = new BufferedReader(fr);
String s;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while ((s=br.readLine())!=null) {
int N = Integer.parseInt(s);
int M = Integer.parseInt(br.readLine());
ArrayList<ArrayList<Doro>> arr = new ArrayList<ArrayList<Doro>>();
for (int i = 0; i <= N; i++) {
arr.add(new ArrayList<Doro>());
}
int a,b,c;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
arr.get(a).add(new Doro(b,c));
arr.get(b).add(new Doro(a,c));
}
//알고리즘 시작
int[] visit = new int[N+1]; // 방문했는지 여부
PriorityQueue<Doro> pq = new PriorityQueue<Doro>(new Comparator<Doro>() {
@Override
public int compare(Doro o1, Doro o2) {
return o1.cost-o2.cost;
}
});
pq.add(new Doro(1,0));
ArrayList<Doro> tempP;
Doro d, d2;
int answer = 0;
while (!pq.isEmpty()) {
d = pq.poll();
if(visit[d.node] == 1) continue;
tempP = arr.get(d.node);
visit[d.node] = 1;
answer += d.cost;
for (int i = 0; i < tempP.size(); i++) {
d2 = tempP.get(i);
if(visit[d2.node] == 0){
pq.add(d2);
}
}
}
boolean exist = true;
for (int i = 1; i <= N; i++) {
if(visit[i] == 0){
exist = false;
break;
}
}
if(!exist) System.out.println(-1);
else
System.out.println(answer);
}
}
}
'Programming > Algorithm' 카테고리의 다른 글
| [DP] 백준 알고리즘 2256번 젓가락 (Fail) (825) | 2016.12.05 |
|---|---|
| [Algorithm] K번째 최단 경로 (Fail) (596) | 2016.12.01 |
| [Algorithm 10일차] 폐지 두번 줍기 (4) | 2016.11.25 |
| [Algorithm 10일차] 최장 증가 부분 수열(LIS) 2 (Fail) (0) | 2016.11.25 |
| [Algorithm 10일차] 마트료시카 (0) | 2016.11.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Git
- 알고리즘
- 혁오
- 마시내 탕수육
- 쿠팡개인정보유출
- 크러쉬
- GraphQL
- s9+
- 중국어강의
- 10cm
- ES6
- Axis2
- 부동산거래계약신고필증
- 부동신 계약시 주의사항
- 로꼬
- 뒤꿈치 건조함
- AWS nodejs
- 고운발크림
- 중국어공부
- 중국어정리
- 서머너즈워
- 쿠팡정보유출
- 프렌즈
- 존맛탱
- AWS npm
- 웰빙헬스
- 자금조달계획서
- 생활코딩
- 프리티어
- 수미네 반찬
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
글 보관함