Skip to content

Instantly share code, notes, and snippets.

@kukukeke
Created March 31, 2018 08:02
Show Gist options
  • Save kukukeke/845d65c1d13a26409a818ee64c339dce to your computer and use it in GitHub Desktop.
Save kukukeke/845d65c1d13a26409a818ee64c339dce to your computer and use it in GitHub Desktop.
// 1197.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <memory.h>
using namespace std;
int sum = 0;
bool taken[10100];
vector<pair<int,int>> adjlist[10100];
priority_queue<pair<int, int>> pq;
int ve, e; int u, w;
void process(int vtx) {
taken[vtx] = true;
for (int j = 0; j < adjlist[vtx].size(); j++) {
pair<int, int> v = adjlist[vtx][j];
if (!taken[v.first]) pq.push(make_pair(-v.second, -v.first));
}
}
int main()
{
freopen("test.txt", "r", stdin);
cin >> ve >> e;
int start; bool rand = false;
for (int i = 0; i < e; i++) {
int a, b, c;
cin >> a >> b >> c;
if (!rand) {
start = a;
rand = true;
}
adjlist[a].push_back(make_pair(b,c));
adjlist[b].push_back(make_pair(a, c));
}
memset(taken, 0, sizeof(bool) * 10100);
process(0);
sum = 0;
while (!pq.empty()) {
pair<int, int> front = pq.top();
pq.pop();
u = -front.second; w = -front.first;
if (!taken[u]) {
sum += w;
process(u);
}
}
cout << sum << "\n";
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment