Pagini recente » Cod sursa (job #454190) | Cod sursa (job #1441747) | Cod sursa (job #2709663) | Cod sursa (job #1966656) | Cod sursa (job #1710133)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cassert>
#define NMAX 250009
using namespace std;
struct edge {
int x, y, c, p;
edge () {}
edge (int _x, int _y, int _c, int _p) : x(_x), y(_y), c(_c), p(_p) {}
bool operator<(const edge& e) const {
if (c != e.c) return c < e.c;
return p < e.p;
}
};
int N, M, D, P, cost, T[ NMAX ];
vector< edge > E;
vector <int> sol;
set<int> rez;
int djset(int x) {
if (T[ x ] != x) {
T[ x ] = djset( T[ x ] );
}
return T[ x ];
}
int reunion(int x, int y) {
T[ djset(x) ] = djset( y );
}
int main() {
freopen("politie.in", "r", stdin);
freopen("politie.out", "w", stdout);
scanf("%d%d%d%d", &N, &M, &D, &P);
assert( 0 <= P && P < N);
while (M--) {
int x, y, c, p;
scanf("%d%d%d%d", &x, &y, &c, &p);
E.push_back( edge(x, y, c, p) );
}
sort(E.begin(), E.end());
for (int i = 1; i <= N; ++i) {
T[ i ] = i;
}
vector<edge>::iterator k;
for (vector<edge>::iterator it = E.begin(); it != E.end(); ++it) {
int x = it->x, y = it->y, c = it->c;
if (djset(x) != djset(y)) {
reunion(x, y);
sol.push_back( it->p );
if (sol.size() == N - 1) {
break;
}
if (rez.size() == P) {
break;
}
}
}
sort(sol.begin(), sol.end());
for (int i = sol.size() - 1; i >= 0 && P; --i) {
if (rez.find(sol[i]) != rez.end()) {
continue;
}
--P;
rez.insert(sol[i]);
printf("%d\n", sol[i]);
}
return 0;
}