Pagini recente » Cod sursa (job #968797) | Cod sursa (job #1952665)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int NMax = 2e5 + 5;
const int inf = 1e9;
typedef long long ll;
int N,M,costArb;
int lung[NMax];
bool inApm[NMax];
struct elem {
int x,y,c;
elem (int _x = 0,int _y = 0,int _c = 0) {
x = _x;
y = _y;
c = _c;
}
};
bool operator <(elem a,elem b) {
return a.c > b.c;
}
vector<elem> v[NMax];
vector<elem> sol;
int main() {
in>>N>>M;
for (int i=1;i<=M;++i) {
int x,y,c;
in>>x>>y>>c;
v[x].push_back(elem(x,y,c));
v[y].push_back(elem(y,x,c));
}
for (int i=2;i<=N;++i) {
lung[i] = inf;
}
lung[1] = 0;
priority_queue<elem> Q;
Q.push(elem(0,1,0));
while (Q.size()) {
elem e = Q.top();
Q.pop();
int nod = e.y;
if (lung[nod] < e.c) {
continue;
}
inApm[nod] = true;
costArb += e.c;
sol.push_back(e);
for (int k=0;k < (int)v[nod].size();++k) {
int next = v[nod][k].y, cost = v[nod][k].c;
if (inApm[next] || lung[next] <= cost) {
continue;
}
lung[next] = cost;
Q.push(elem(nod,next,cost));
}
}
out<<costArb<<'\n'<<sol.size()-1<<'\n';
for (int k=1;k < (int)sol.size();++k) {
out<<sol[k].x<<' '<<sol[k].y<<'\n';
}
in.close();out.close();
return 0;
}