Pagini recente » Cod sursa (job #2237332) | Cod sursa (job #1920582) | Cod sursa (job #1533166) | Monitorul de evaluare | Cod sursa (job #2487252)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int DIM = 1e6;
vector < pair <int, int > > L[DIM];
bool viz[DIM];
int Ex[DIM];
int n,m;
vector < pair <int, int> > Rez;
struct no
{
int first,second;
};
struct compara
{
int operator() (no x, no y)
{
return L[x.first][x.second].second > L[y.first][y.second].second;
}
};
priority_queue <pair < int, pair <int, int> > > Coada;
int prim()
{
int x = 1;
viz[x] = true;
for(int i = 0 ;i < L[x].size(); i++)
{
int poz = L[x][i].first;
int c = L[x][i].second;
Coada.push(make_pair(-c,make_pair(x,poz)));
}
int nr = 1;
int cost = 0;
while( nr < n && !Coada.empty())
{
while(viz[Coada.top().second.second])
Coada.pop();
int nod2 = Coada.top().second.first;
int nod = Coada.top().second.second;
int c = Coada.top().first;
nr++;
cost += c;
viz[nod] = true;
Rez.push_back( make_pair(nod2,nod) );
Coada.pop();
for(int i = 0 ;i < L[nod].size(); i++)
{
int poz = L[nod][i].first;
int c = L[nod][i].second;
if(viz[poz] == false)
Coada.push(make_pair(-c,make_pair(nod,poz)));
}
}
out << -cost <<'\n' << nr - 1 <<'\n';
for(int i = 0 ;i < Rez.size(); i++)
out << Rez[i].first <<" "<< Rez[i].second<<'\n';
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m ;i++)
{
int x, y, c;
in >> x >> y >> c;
L[x].push_back(make_pair(y,c));
L[y].push_back(make_pair(x,c));
}
prim();
return 0;
}