Pagini recente » Cod sursa (job #1400835) | Cod sursa (job #1447972) | Cod sursa (job #332769) | Cod sursa (job #1005169) | Cod sursa (job #2468993)
#include <fstream>
#include <vector>
#include <queue>
#define inf 20001
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int d[200001];
int tata[200001];
int n,m,x,y,c,Min,s;
bool viz[200001];
struct cmp
{
bool operator ()(const int &a, const int &b)
{
return d[a]>d[b];
}
};
vector <pair <int, int > > g[200001];
priority_queue < int , vector < int> ,cmp > h;
int main()
{
fin>>n>>m;
for(int 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));
}
for(int i=2;i<=n;i++){
d[i]=inf;
}
h.push(1);
int k=0;
while(!h.empty())
{
int nod=h.top();
h.pop();
if(viz[nod]==1)
continue;
viz[nod]=1;
s=s+d[nod];
for(int j=0;j<g[nod].size();j++)
{
int v=g[nod][j].first;
int c=g[nod][j].second;
if(d[v]>c&&viz[v]==0)
{
d[v]=c;
tata[v]=nod;
h.push(v);
}
}
}
fout<<s<<'\n'<<n-1<<'\n';
for(int i=2;i<=n;i++)
fout<<tata[i]<<' '<<i<<'\n';
return 0;
}