Problem-Solving/BOJ
[BOJ/백준][Gold4] 13424 : 비밀 모임 (C++)
has2
2021. 12. 18. 21:51
https://www.acmicpc.net/problem/13424
13424번: 비밀 모임
입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에 테스트 케이스의 개수를 나타내는 자연수 T가 주어진다. 각 테스트 케이스의 첫째 줄에는 방
www.acmicpc.net
[난이도] Gold4
[유형] 플로이드 와샬
[풀이]
플로이드 와샬을 이용해 각 지점간 거리를 구해놓으면 정답을 쉽게 구할 수 있습니다.
#include <cstdio>
#include <algorithm>
using namespace std;
int T,N,M,K,dist[101][101],inf=9e8,frd[101];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++) {
for(int j=1;j<=N;j++) {
dist[i][j]=inf;
if(i==j) dist[i][j]=0;
}
}
while(M--){
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
dist[v][u]=dist[u][v]=d;
}
scanf("%d",&K);
for(int i=0;i<K;i++) scanf("%d",&frd[i]);
for(int k=1;k<=N;k++){
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
dist[i][j] = min(dist[i][k]+dist[k][j],dist[i][j]);
}
}
}
int ans=inf,idx=-1;
for(int i=1;i<=N;i++){
int v=0;
for(int j=0;j<K;j++) v+=dist[i][frd[j]];
if(ans>v){
idx=i;
ans=v;
}
}
printf("%d\n",idx);
}
}
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold4/13424.cpp