Pagini recente » Cod sursa (job #2685627) | Cod sursa (job #1457663) | Monitorul de evaluare | Cod sursa (job #1731219) | Cod sursa (job #2507515)
#include <bits/stdc++.h>
#define Roc ios_base::sync_with_stdio(false), cin.tie(NULL)
#define ll int
#define NMAX 200009
#define pi pair<ll,ll>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
typedef pair<ll, pi> mch;
vector <pi> G[NMAX];
vector <pi> sol;
ll n, m, x, y, c,cat;
bitset <NMAX> viz;
priority_queue<mch, vector<mch>, greater<mch> > Q;
int main()
{
Roc;
f >> n >> m;
while(f >> x >> y >> c)
{
G[x].push_back({y,c});
G[y].push_back({x,c});
}
for(auto it : G[1])
Q.push({it.second, {1, it.first} });
viz[1] = 1;
while(!Q.empty())
{
mch act = Q.top();
Q.pop();
if(!viz[act.second.second])
{
cat += act.first;
sol.push_back({act.second.first, act.second.second});
viz[ act.second.second ] = 1;
for(auto it : G[act.second.second])
if(!viz[it.first])
Q.push({it.second, {act.second.second, it.first} });
}
}
g << cat << '\n';
g << sol.size() << '\n';
for(auto it : sol)
g << it.first << ' ' << it.second << '\n';
f.close();
g.close();
return 0;
}