Pagini recente » Cod sursa (job #701521) | Cod sursa (job #3202521) | Cod sursa (job #1255818) | Cod sursa (job #1601313) | Cod sursa (job #3202689)
#include <fstream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n,m;
const int nmax = 200000;
const int INF = 1e9;
vector <pair<int,int>> v[nmax + 5];
bool viz[nmax + 5];
int d[nmax + 5];
int t[nmax + 5];
struct elem{
int nod;
int c;
bool operator<(const elem& b) const
{
return c>b.c;
}
};
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,c;
fin>>x>>y>>c;
v[x].push_back({c,y});
v[y].push_back({c,x});
}
for(int i=1;i<=n;i++)
d[i]=INF;
d[1]=0;
priority_queue <elem> q;
q.push({1,0});
while(!q.empty())
{
int fr= q.top().nod;
q.pop();
if(viz[fr])
continue;
viz[fr]=true;
for(auto& i : v[fr])
if(i.first < d[i.second] && !viz[i.second]){
d[i.second]=i.first,q.push({i.second,i.first});
t[i.second]=fr;
}
}
int s=0;
for(int i=1;i<=n;i++)
s+=d[i];
fout<<s<<'\n';
fout<<n-1<<'\n';
for(int i=2;i<=n;i++)
fout<<t[i]<<' '<<i<<'\n';
}