Pagini recente » Istoria paginii runda/dormeau_adanc | Istoria paginii utilizator/leon_baro-san | Istoria paginii runda/0987654321 | Monitorul de evaluare | Cod sursa (job #2540945)
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
auto cmp = [] (pair<int,int> a, pair<int,int> b)
{
return a.second > b.second;
};
priority_queue <pair<int,int>, vector<pair<int,int>>, decltype(cmp)> pq(cmp);
int n,m,i,x,y,a,b,pret,total,cost[200001],tata[200001];
vector <vector<pair<int,int>>> v;
bitset <200001> trecut;
int main()
{
f >> n >> m;
v.resize(n + 1);
while(m --)
{
f >> x >> y >> pret;
v[x].push_back({y,pret});
v[y].push_back({x,pret});
}
for(i = 1; i <= n; ++ i)
cost[i] = inf;
for(i = 0; i < v[1].size(); ++ i)
{
a = v[1][i].first;
b = v[1][i].second;
cost[a] = b;
tata[a] = 1;
pq.push({a,b});
}
tata[1] = cost[1] = 0;
trecut[1] = 1;
while(!pq.empty())
{
a = pq.top().first;
b = pq.top().second;
pq.pop();
if(trecut[a])
continue;
trecut[a] = 1;
total += b;
for(i = 0; i < v[a].size(); ++ i)
{
x = v[a][i].first;
y = v[a][i].second;
if(!trecut[x] && cost[x] > y)
{
cost[x] = y;
tata[x] = a;
pq.push({x,y});
}
}
}
g << total << '\n' << n - 1 << '\n';
for(i = 2; i <= n; ++ i)
g << i << ' ' << tata[i] << '\n';
}