Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 원판 돌리기
- 풀이
- 1764
- 버킷 정렬
- qorwns
- 게리멘더링2
- 백준 17779
- 스택의 특징
- 백준
- C/C++ 구현
- 5397
- 백준 5397
- heap
- ㅣ풀이
- 해시 구현
- 별 찍기 10
- 시간 복잡도
- 백준 17822
- dfs
- 해시구현
- AVL 시간 복잡도
- 조세퍼스 순열
- 구현
- 백준 2447
- Stack 이란
- 백준 1158
- 백준 1406
- 자료구조
- c#
- 백준 17471
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1238] 파티 본문
https://www.acmicpc.net/problem/1238
1238번: 파티
문제 N개의 숫자로 구분된 각각의 마을에 한 명의 학생이 살고 있다. 어느 날 이 N명의 학생이 X (1 ≤ X ≤ N)번 마을에 모여서 파티를 벌이기로 했다. 이 마을 사이에는 총 M개의 단방향 도로들이 있고 i번째 길을 지나는데 Ti(1 ≤ Ti ≤ 100)의 시간을 소비한다. 각각의 학생들은 파티에 참석하기 위해 걸어가서 다시 그들의 마을로 돌아와야 한다. 하지만 이 학생들은 워낙 게을러서 최단 시간에 오고 가기를 원한다. 이 도로들은 단방향이기 때
www.acmicpc.net
요구사항
왕복이 가장 오래 걸리는 학생 찾기
풀이
다익스트라 알고리즘을 2번 써줘서
집-> 파티장 최단거리
파티장 -> 집 최단거리
를 구해서 가장 오래 걸리는 학생을 찾아준다
#include <stdio.h>
#include <iostream>
#include <queue>
#include <functional>
#include <vector>
#include <string.h>
using namespace std;
int n,m,k;
int dist[1001];
int dist1[1001];
struct go{
int x;
int y;
};
vector<go> v[20001];
int main()
{
cin>>n>>m>>k;
int ans=0;
for(int i=0 ; i < m ; i++){
int x, y,z;
scanf("%d%d%d",&x,&y,&z);
v[x].push_back({y,z});
}
for(int i=1; i<=n ; i++){
for(int i=1;i<=n;i++){
dist[i]=987654321;
dist1[i]=987654321;
}
dist[i]=0;
dist1[k]=0;
priority_queue<pair<int,int>> q;
priority_queue<pair<int,int>> q1;
q.push({0,i});
q1.push({0,k});
while(!q.empty())
{
int now = q.top().second;
int nowdist= -q.top().first;
q.pop();
for(int i=0 ; i < v[now].size();i++){
int next = v[now][i].x;
int d = nowdist +v[now][i].y;
// cout<<"d"<<d<<endl;
if( dist[next] > d){
// cout<<"A"<<endl;
dist[next]=d;
q.push({-dist[next],next});
}
}
}
while(!q1.empty())
{
int now = q1.top().second;
int nowdist= -q1.top().first;
q1.pop();
for(int i=0 ; i < v[now].size();i++){
int next = v[now][i].x;
int d = nowdist +v[now][i].y;
if( dist1[next] > d){
dist1[next]=d;
q1.push({-dist1[next],next});
}
}
}
int tdist=dist[k]+dist1[i];
if(tdist>ans)
ans=tdist;
// cout<<"k"<<k<<"i"<<i<<endl;
// cout<<i<<" "<<dist[k]<< " " <<dist1[i]<< endl;
}
cout<<ans<<endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 4485] 젤다 (0) | 2020.01.14 |
---|---|
[백준 1261] 알고스팟 (0) | 2020.01.14 |
[백준 1987] 알파벳 (0) | 2020.01.13 |
[백준 2468] 안전 영역 (0) | 2020.01.13 |
[백준 2583] 영역 구하기 (0) | 2020.01.13 |
Comments