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