Created
March 4, 2017 16:10
-
-
Save ia7ck/b5c9f16538b86e012ff5dc61de261ce5 to your computer and use it in GitHub Desktop.
ARC 029-C 高橋君と国家
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<vector> | |
#include<queue> | |
using namespace std; | |
#define int long long | |
struct Edge{ | |
int to, c; | |
bool operator<(const Edge& hoge)const{ | |
return c>hoge.c; | |
} | |
}; | |
vector<Edge> g[100000+1]; | |
int used[100000+1]; | |
signed main(){ | |
int N, M; | |
cin>> N>> M; | |
for(int i=0; i<N; i++){ | |
int c; | |
cin>> c; | |
g[i].push_back(Edge{N, c}); | |
g[N].push_back(Edge{i, c}); | |
} | |
for(int i=0; i<M; i++){ | |
int a, b, r; | |
cin>> a>> b>> r; | |
a--; b--; | |
g[a].push_back(Edge{b, r}); | |
g[b].push_back(Edge{a, r}); | |
} | |
priority_queue<Edge> Q; | |
for(auto e: g[0]) Q.push(e); | |
used[0]=1; | |
int c=0; | |
while(!Q.empty()){ | |
auto e=Q.top(); Q.pop(); | |
if(used[e.to]) continue; | |
c+=e.c; | |
used[e.to]=1; | |
for(auto ne: g[e.to]){ | |
if(used[ne.to]) continue; | |
Q.push(ne); | |
} | |
} | |
cout<< c<< endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment