This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> g(n);
for(int i= 0;i<n;i++) cin >> g[i];
vector<int> dist(n,INT32_MAX);
vector<bool> inMST(n,false);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
dist[0] = 0;
int ans = 0;
pq.push({dist[0],0});
while(!pq.empty()){
pair<int,int> t = pq.top();
pq.pop();
int w = t.first, v = t.second;
if(!inMST[v]){
ans += dist[v];
inMST[v] = true;
for(int i = 0;i<n;i++){
if(i != v && g[i] + g[v] < dist[i]){
dist[i] = g[i] + g[v];
pq.push({dist[i],i});
}
}
}
}
cout << ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:21:13: warning: unused variable 'w' [-Wunused-variable]
21 | int w = t.first, v = t.second;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |