Pagini recente » Cod sursa (job #2854849) | Cod sursa (job #635525) | Cod sursa (job #1963341) | Cod sursa (job #3281373) | Cod sursa (job #1663223)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
const int nmax = 2e5+5;
const int oo = 2e9;
vector <pair<int,int> > g[nmax], apm;
bitset <nmax> viz;
int n, m, dist[nmax], cmin;
struct heap_node{
int dad, node, cost;
};
class cmp{
public:
inline bool operator () (const heap_node &x, const heap_node &y){
return x.cost > y.cost;
}
};
void Prim(){
priority_queue <heap_node, vector<heap_node>, cmp> heap;
vector <pair<int,int> > :: iterator it;
int i, dad, node, son, cost;
for(i=2; i<=n; i++)
dist[i]=oo;
dist[1]=0;
heap.push({0, 1, 0});
while(!heap.empty()){
dad=heap.top().dad;
node=heap.top().node;
cost=heap.top().cost;
heap.pop();
if(node!=1 && viz[node]==false){
cmin+=cost;
apm.push_back({dad, node});
}
viz[node]=true;
for(it=g[node].begin(); it!=g[node].end(); it++){
son=it->first;
cost=it->second;
if(viz[son]==false && dist[son] > cost){
dist[son]=cost;
heap.push({node, son, cost});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
int x, y, c, i;
fin >> n >> m;
for(i=1; i<=m; i++){
fin >> x >> y >> c;
g[x].push_back({y, c});
g[y].push_back({x, c});
}
Prim();
fout << cmin << "\n" << n-1 << "\n";
for(i=0; i<apm.size(); i++)
fout << apm[i].first << " " << apm[i].second << "\n";
fin.close();
fout.close();
return 0;
}