Pagini recente » Profil alexxx92 | Cod sursa (job #1194126) | Cod sursa (job #1479015) | Cod sursa (job #619992) | Cod sursa (job #2204286)
#include <bits/stdc++.h>
using namespace std;
struct edge{
int x, y, c;
bool operator<(const edge &obj)const{
return c < obj.c;
}
};
int n, m;
bool ok[200005];
multiset<edge> q;
vector<edge> v[200005], sol;
edge make_edge(int x, int y, int c)
{
edge aux;
aux.x = x;
aux.y = y;
aux.c = c;
return aux;
}
int main()
{
ifstream fin ("apm.in");
ofstream fout ("apm.out");
fin >> n >> m;
for (int i = 1; i <= m; ++i){
int x, y, z;
fin >> x >> y >> z;
v[x].push_back(make_edge(x, y, z));
v[y].push_back(make_edge(x, y, z));
}
for (vector<edge>::iterator it = v[1].begin(); it != v[1].end(); ++it)
q.insert(*it);
int apm = 0;
for (int t = 1; t < n; ++t){
while(ok[q.begin()->x] && ok[q.begin()->y])
q.erase(q.begin());
int x = q.begin()->x, y = q.begin()->y, c = q.begin()->c;
sol.push_back(*q.begin());
apm += c;
q.erase(q.begin());
if(!ok[x]){
for (vector<edge>::iterator it = v[x].begin(); it != v[x].end(); ++it)
q.insert(*it);
}
if(!ok[y]){
for (vector<edge>::iterator it = v[y].begin(); it != v[y].end(); ++it)
q.insert(*it);
}
ok[x] = ok[y] = 1;
}
fout << apm << "\n" << n-1 << "\n";
for (int i = 0; i < n-1; ++i)
fout << sol[i].x << " " << sol[i].y << "\n";
return 0;
}