https://www.acmicpc.net/problem/17182

 

17182번: 우주 탐사선

우주 탐사선 ana호는 어떤 행성계를 탐사하기 위해 발사된다. 모든 행성을 탐사하는데 걸리는 최소 시간을 계산하려 한다. 입력으로는 ana호가 탐색할 행성의 개수와 ana호가 발사되는 행성의 위

www.acmicpc.net

 

 

[난이도] Gold2
[유형] 플로이드 와샬

[풀이]
플로이드 와샬을 이용해 우주선간 최단 거리를 미리 구해놓은 뒤
방문할 순서를 순열을 이용해 구해준 뒤 각 N!개의 순열에 대해 최단 거리를 계산해주면 됩니다.

 

#include <cstdio>
#include <algorithm>
using namespace std;
int N,K,dist[11][11],seq[11],ans=9e8;
int main(){
    scanf("%d%d",&N,&K);
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++) scanf("%d",&dist[i][j]);

    for(int k=0;k<N;k++)
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
    int j=0;
    for(int i=0;i<N;i++) if(i!=K) seq[j++]=i;
    do{ 
        int t=dist[K][seq[0]];
        for(int i=1;i<N-1;i++) t+=dist[seq[i-1]][seq[i]];
        ans=min(t,ans);
    }while(next_permutation(seq,seq+N-1));
    printf("%d",ans); 
}


https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold2/17182.cpp

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

+ Recent posts