Cod sursa(job #1709937)

Utilizator UTCN_heapstrUTCN HeapStr UTCN_heapstr Data 28 mai 2016 14:31:47
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.46 kb
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <set>

using namespace std;

typedef struct {
	int src;
	int dst;
	int t, c;
} Edge;


vector<Edge> edges;

struct Compfunc
{
	bool operator ()(const Edge &a, const Edge &b)
	{
		if (a.t == b.t) {
			return a.c > b.c;
		}

		return a.t > b.t;
	}
};

struct Comp2
{
	bool operator ()(const int &a, const int &b)
	{
		return a > b;
	}
};



int n, m, d, p, x, y, t, c;
bool viz[250001];
vector<Edge> neighb[250001];
Edge e;
set<int, Comp2> per;
int cnt = 0;


bool sortFunc(const Edge &a, const Edge &b)
{
	if (a.t == b.t) {
		return a.c < b.c;
	}

	return a.t < b.t;
}


void prim(int x) {
	priority_queue<Edge, vector<Edge>, Compfunc> pq;
	Edge e;

	viz[x] = true;
	cnt++;
	for (int i = 0; i < neighb[x].size(); i++) {
		pq.push(neighb[x][i]);
	}

	while (!pq.empty() && cnt<n) {
		e = pq.top(); pq.pop();
		if (!viz[e.dst]) {
			viz[e.dst] = true;
			cnt++;
			per.insert(e.c);
			for (int i = 0; i < neighb[e.dst].size(); i++) {
				if (!viz[neighb[e.dst][i].dst]) {
					pq.push(neighb[e.dst][i]);
				}
			}
		}
	}
}


int parent[250001];
int rankVal[250001];


int find(int node){

	if (parent[node] == node){
		return node;
	}

	parent[node] = find(parent[node]);

	return parent[node];
}

void unionFunc(int n1, int n2){

	if (rankVal[n1] > rankVal[n2]){
		parent[n2] = n1;
	}
	else if (rankVal[n1] < rankVal[n2]){
		parent[n1] = n2;
	}
	else {
		parent[n2] = n1;
		rankVal[n1]++;
	}
}

void kruskal(){
	sort(edges.begin(), edges.end(), sortFunc);

	for (int i = 1; i <= n; i++){
		parent[i] = i;
		rankVal[i] = 0;
	}

	for (int i = 0; i < edges.size(); i++){
		int c1 = find(edges[i].src);
		int c2 = find(edges[i].dst);

		if (c1 != c2){
			per.insert(edges[i].c);

			unionFunc(c1, c2);
		}
	}

}

int main(){
	freopen("politie.in", "r", stdin);
	freopen("politie.out", "w", stdout);

	scanf("%d%d%d%d", &n, &m, &d, &p);

	edges.reserve(m);

	for (int i = 0; i<m; i++) {
		scanf("%d%d%d%d", &x, &y, &t, &c);
		e.dst = y;
		e.t = t;
		e.c = c;		
		e.src = x;

		edges.push_back(e);
	}

	//prim(1);
	kruskal();


	int i = 0;
	for (set<int>::iterator it = per.begin(); it != per.end() && i < p; i++, it++) {
		printf("%d\n", *it);
	}

	return 0;
}