#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
 
int N, M;
vector<pair<intint>> pos;
double v[1010][1010];
int visited[1010];
double cost;
 
void prim()
{    
    priority_queue<pair<doubleint>vector<pair<doubleint>>, greater<pair<doubleint>>> pq;
    visited[1= 1;    
    for(int i = 2; i <= N; i++)
    {
        int next = i;
        double nextCost = v[1][i];
        
        pq.push({nextCost, next});
    }
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        double nowCost = pq.top().first;        
        pq.pop();
        
        if(visited[now] == 0)
        {
            visited[now] = 1;
            cost += nowCost;
        } 
        else
        {
            continue;
        }
        
        for(int i = 1; i <= N; i++)
        {
            int next = i;
            double nextCost = v[now][i];
            
            if(visited[next] == 0)
            {
                pq.push({nextCost, next});
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1774_input.txt", "r", stdin);
    
    cin >> N >> M;
    
    for(int i = 1; i <= N; i++)
    {
        int x, y;
        cin >> x >> y;
        
        pos.push_back({x, y});    
    }
    
    for(int i = 0; i < pos.size(); i++)
    {
        for(int j = 0; j < pos.size(); j++)
        {
            if(i == j)
            {
                continue;
            }
            
            double weight = sqrt(pow(pos[i].first-pos[j].first, 2.0+ pow(pos[i].second-pos[j].second, 2.0));    
            
            v[i+1][j+1= weight;
        }
    }
    
    for(int i = 1; i <= M; i++)
    {
        int from, to;
        cin >> from >> to;
        
        // 연결된 정점의 비용을 0으로 초기화 
        v[from][to] = 0;
        v[to][from] = 0;
    }
 
    prim();
    
    printf("%.2lf", cost);
        
    return 0;
}
cs

'Baekjoon > MST' 카테고리의 다른 글

[백준 1197] 최소 스패닝 트리 (Prim) (C/C++)  (0) 2020.05.17
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
 
int V, E;
vector<pair<intint>> v[10010];
int visited[10010];
int cost;
 
void prim()
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    visited[1= 1;
    for(int i = 0; i < v[1].size(); i++)
    {
        pq.push({v[1][i].second, v[1][i].first});
    }
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;        
        pq.pop();
        
        if(visited[now] == 0)
        {
            visited[now] = 1;
            cost += nowCost;
        }
        else
        {
            continue;
        }
        
        for(int i = 0; i < v[now].size(); i++)
        {
            int next = v[now][i].first;
            int nextCost = v[now][i].second;
            
            if(visited[next] == 0)
            {
                pq.push({nextCost, next});
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1197_input.txt", "r", stdin);
    
    cin >> V >> E;
    
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        // 양방향 삽입 
        v[from].push_back({to, weight});
        v[to].push_back({from, weight});    
    }
    
    prim();
    
    cout << cost << endl;
    
    return 0;
}
cs

'Baekjoon > MST' 카테고리의 다른 글

[백준 1774] 우주선과의 교감 (Prim) (C/C++) (★★)  (0) 2020.05.18

+ Recent posts