Pagini recente » Cod sursa (job #848807) | Cod sursa (job #2969872) | Cod sursa (job #485859) | Cod sursa (job #2075637) | Cod sursa (job #2716314)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
int sum, nrsol, cost[200005], sol[200005];
vector <pair <int, int> > e[200005];
priority_queue <pair <int, int>, vector<pair <int, int> >, greater<pair <int, int> > > nodes;
bitset <200005> vis;
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++i)
cost[i] = 1000000137;
for(int i = 1; i <= m; ++i)
{
int a, b, c;
cin >> a >> b >> c;
e[a].push_back({b, c});
e[b].push_back({a, c});
}
nodes.push({0, 1});
while (!nodes.empty())
{
int i = nodes.top().second;
nodes.pop();
if(vis[i]==true)
continue;
vis[i] = true;
for(auto it = e[i].begin(); it != e[i].end(); ++it)
{
if(sol[i] == it->first)
continue;
if(cost[it->first] > it->second && vis[it->first] == false)
{
if(cost[it->first] != 1000000137)
sum -= cost[it->first];
else ++nrsol;
sum += it->second;
sol[it->first] = i;
cost[it->first] = it->second;
nodes.push({cost[it->first], it->first});
}
}
}
cout << sum << '\n' << nrsol << '\n';
for(int i = 2; i <= n; ++i)
{
cout << sol[i] << ' ' << i << '\n';
}
return 0;
}