Pagini recente » Cod sursa (job #2641205) | Cod sursa (job #3234601) | Cod sursa (job #1923260) | Cod sursa (job #2941390) | Cod sursa (job #2195160)
#include <fstream>
#include <queue>
#include <climits>
#include <vector>
#define nmax 200005
using namespace std;
class muchie
{
public:
int u,v,cost;
bool operator()(const muchie &p1, const muchie &p2)
{
return p1.cost>p2.cost;
}
muchie(int a=0,int b=0,int c=0): u(a),v(b),cost(c){}
};
priority_queue <muchie, vector <muchie>, muchie> q;
vector <muchie> a[nmax];
vector <muchie> alese;
bool viz[nmax];
int ct;
void solve()
{
muchie x,y;
unsigned int i;
while (!q.empty())
{
x=q.top();
q.pop();
if ((!viz[x.v]))
{
viz[x.v]=true;
ct+=x.cost;
alese.push_back(muchie(x.u,x.v,0));
for (i=0;i<a[x.v].size();i++)
{
y.u=x.v;
y.v=a[x.v][i].u;
y.cost=a[x.v][i].cost;
if(!viz[y.v])
q.push(y);
}
}
}
}
int main()
{
ifstream fin("apm.in");
ofstream fout("apm.out");
int x,y,c;
unsigned int i,n,m;
fin>>n>>m;
for (i=1;i<=m;i++)
{
fin>>x>>y>>c;
a[x].push_back(muchie(y,0,c));
a[y].push_back(muchie(x,0,c));
}
q.push(muchie(0,1,0));
solve();
fout<<ct<<'\n';
fout<<alese.size()-1<<'\n';
for (i=1;i<alese.size();i++)
{
fout<<alese[i].u<<' '<<alese[i].v<<'\n';
}
fin.close();
fout.close();
return 0;
}