본문 바로가기

코딩/알고리즘

SW expert academy 1251 하나로

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <utility>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <math.h>
#include <string>
 
using namespace std;
 
int N;
 
double E;
 
long long node[1000][2]; //i, j 저장
 
long long adj[1000][1000]; // i <-> j의 비용
 
 
int main() {
 
    int tcm;
    scanf("%d", &tcm);
    for (int tc = 1; tc <= tcm; tc++) {
        scanf("%d", &N);
 
        for (int i = 0; i < N; i++) {
            scanf("%lld", &node[i][0]);
        }
        for (int i = 0; i < N; i++) {
            scanf("%lld", &node[i][1]);
        }
 
        scanf("%lf", &E);
 
        for (int i = 0; i < N; i++) {
            long long nowi = node[i][0];
            long long nowj = node[i][1];
 
            for (int j = 0; j < N; j++) {
                if (i == j) continue;
                long long nexti = node[j][0];
                long long nextj = node[j][1];
                long long weight = ((nowi - nexti) * (nowi - nexti)) + (nowj - nextj) * (nowj - nextj);
                adj[i][j] = weight;
                adj[j][i] = weight;
            }
        }
 
        vector<long long> isVisit = vector<long long>();
        bool isVisitArr[1000];
        for (int i = 0; i < 1000; i++) {
            isVisitArr[i] = false;
        }
        isVisit.push_back(0);
        isVisitArr[0] = true;
 
        long long allWeight = 0;
 
        while (isVisit.size() < N) {
            int next = 0;
            long long minWeight = -1;
            for (auto now : isVisit) {
                for (int i = 0; i < N; i++) {
                    if (i == now) continue;
                    if (isVisitArr[i]) continue;
                    if (minWeight == -1 || adj[now][i] < minWeight) {
                        minWeight = adj[now][i];
                        next = i;
                    }
                }
            }
            allWeight += minWeight;
            //cout << "MIN : " << minWeight << " , ALL : " << allWeight << endl;
            isVisitArr[next] = true;
            isVisit.push_back(next);
        }
 
        printf("#%d %lld\n", tc, (long long)round(E * allWeight));
 
        cout << "";
    }
 
    return 0;
}

'코딩 > 알고리즘' 카테고리의 다른 글

SW expert academy 1238 Contact  (0) 2019.04.13
SW expert academy 1227 미로2  (0) 2019.04.13
백준 17069 파이프 옮기기2, 17070 파이프 옮기기1  (0) 2019.04.13
백준 1315 RPG  (0) 2018.06.03
백준 11062 카드게임  (1) 2018.06.03