티스토리 뷰
http://www.koitp.org/problem/KTHSHORTEST/read/
시간 제한 | 메모리 제한 | 제출 횟수 | 정답 횟수 (비율) | 정답자 수 |
---|---|---|---|---|
3.0 초 | 512 MB | 663 | 109 (16%) | 77 |
>> 풀이법은 맞다고보는데, 0.2~0.3 초 차이로 시간초과가 남.
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 7
5 7 2
1 2 2
1 3 5
1 4 1
2 3 2
3 4 5
2 5 6
3 5 1
##
예제 출력
6
6
*/
class Path{
int node;
long cost;
public Path(int node, long cost) {
super();
this.node = node;
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) {
st = new StringTokenizer(s);
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
ArrayList<ArrayList<Path>> arr = new ArrayList<ArrayList<Path>>();
ArrayList<PriorityQueue<Long>> kThCost = new ArrayList<PriorityQueue<Long>>();
for (int i = 0; i <= N; i++) {
arr.add(new ArrayList<Path>());
kThCost.add(new PriorityQueue<Long>());
}
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 Path(b,c));
arr.get(b).add(new Path(a,c));
}
//알고리즘 시작.
PriorityQueue<Path> pq = new PriorityQueue<Path>(new Comparator<Path>() {
@Override
public int compare(Path o1, Path o2) {
if(o2.cost<o1.cost) return -1;
else if(o2.cost>o1.cost) return 1;
else return 0;
}
});
pq.add(new Path(1,0)); // 1번 노드에서 출발
Path p;
PriorityQueue<Long> tempPq;
ArrayList<Path> tempList;
long d;
while (!pq.isEmpty()) {
p = pq.poll();
tempPq = kThCost.get(p.node);
if(tempPq.size() == K){
if(tempPq.peek() >= p.cost){
continue;
}
tempPq.poll();
}
tempPq.add(p.cost);
d = p.cost;
tempList = arr.get(p.node);
for (int i = 0; i < tempList.size(); i++) {
p = tempList.get(i);
pq.add(new Path(p.node, -p.cost+d));
}
}
tempPq = kThCost.get(N);
if(tempPq.size() < K) bw.write("-1\n");
else{
bw.write((-tempPq.poll())+"\n");
}
}
bw.flush();
bw.close();
br.close();
}
}
'Programming > Algorithm' 카테고리의 다른 글
[Algorithm] 백준 알고리즘 9521번 색칠 공부 (262) | 2016.12.12 |
---|---|
[DP] 백준 알고리즘 2256번 젓가락 (Fail) (825) | 2016.12.05 |
[Algorithm] 고속도로 건설 (595) | 2016.12.01 |
[Algorithm 10일차] 폐지 두번 줍기 (4) | 2016.11.25 |
[Algorithm 10일차] 최장 증가 부분 수열(LIS) 2 (Fail) (0) | 2016.11.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 뒤꿈치 건조함
- 존맛탱
- 부동산거래계약신고필증
- 중국어강의
- Git
- Bitnami
- 부동신 계약시 주의사항
- AWS npm
- 알고리즘
- 수미네 반찬
- 생활코딩
- 서머너즈워
- s9+
- 웰빙헬스
- 마시내 탕수육
- 프렌즈
- GraphQL
- 10cm
- AWS nodejs
- 노브랜드
- 중국어공부
- 고운발크림
- Axis2
- 중국어정리
- 프리티어
- 혁오
- 로꼬
- 자금조달계획서
- ES6
- 크러쉬
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함