Pagini recente » Cod sursa (job #585110) | Cod sursa (job #3242977)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int INF = 1e9;
const int nmax = 200000;
int n,m;
bool viz[nmax + 5];
int d[nmax + 5];
int t[nmax + 5];
int sum;
int solsz;
pair<int,int> sol[nmax*2 + 5];
vector <pair<int,int>> v[nmax + 5];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
int main()
{
fin>>n>>m;
for(int i=1; i<=n; i++)
d[i] = INF;
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});
}
d[1]=0;
q.push({0,1});
while(!q.empty())
{
int mc = q.top().first;
int nod = q.top().second;
q.pop();
if(viz[nod]) continue;
viz[nod]=true;
for(auto& i : v[nod])
if(i.first < d[i.second] && !viz[i.second])
{
d[i.second] = i.first,t[i.second] = nod;
q.push({i.first,i.second});
}
}
for(int i=2;i<=n;i++)
sum+=d[i];
fout<<sum<<'\n';
fout<<n-1<<'\n';
for(int i=2;i<=n;i++)
fout<<i<<' '<<t[i]<<'\n';
}