Pagini recente » Cod sursa (job #2949025) | Cod sursa (job #547585) | Cod sursa (job #1276755) | Cod sursa (job #315518) | Cod sursa (job #2025168)
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
using namespace std;
const int nMax = 200005;
int n, m;
vector<pair<int, int> > vecini[nMax];
bool added[nMax];
vector<pair<int, int> > ans;
int ansCost;
void citire()
{
ifstream in("apm.in");
in >> n >> m;
int x, y, c;
for(int i = 1; i <= m; ++i)
{
in >> x >> y >> c;
vecini[x].push_back(make_pair(c, y));
vecini[y].push_back(make_pair(c, x));
}
in.close();
}
void rezolvare()
{
set<pair<int, pair<int, int> > > s;
added[1] = true;
for(auto v:vecini[1])
s.insert(make_pair(v.first, make_pair(1, v.second)));
pair<int, pair<int, int> > p;
while(s.empty() == false)
{
while(s.empty() == false && added[s.begin()->second.second] == true)
s.erase(s.begin());
if(s.empty() == false)
{
p = *(s.begin());
ansCost += p.first;
ans.push_back(p.second);
added[p.second.second] = true;
for(const auto &v:vecini[p.second.second])
if(added[v.second] == false)
s.insert(make_pair(v.first, make_pair(p.second.second, v.second)));
}
}
}
void afisare()
{
ofstream out("apm.out");
out << ansCost << "\n" << n-1 << "\n";
for(auto x:ans)
out << x.first << " " << x.second << "\n";
out.close();
}
int main()
{
citire();
rezolvare();
afisare();
return 0;
}