Cod sursa(job #1712844)

Utilizator antanaAntonia Boca antana Data 3 iunie 2016 20:59:08
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.32 kb
#include <cstdio>
#include<algorithm>
#define MAXN 250000
#define MAXM 500000
using namespace std;
struct muchie{
    int tip, cost, x, y;
}v[MAXM+1];
int p[MAXN+2], t[MAXN+1];
bool cresc(const muchie a, const muchie b)
{
    if(a.tip<b.tip)
        return true;
    if(a.tip>b.tip)
        return false;
    return (a.cost<b.cost);
}
int father(int x)
{
    if(t[x]==x)
        return x;
    return t[x]=father(t[x]);
}
inline void join(int x, int y)
{
    int rx=father(x), ry=father(y);
    t[rx]=ry;
}
bool descr(const int a, const int b)
{
    return (a>b);
}
int main()
{
    freopen("politie.in", "r", stdin);
    freopen("politie.out", "w", stdout);
    int n, m, d, pr, nr=0, secv=0;
    scanf("%d%d%d%d", &n, &m, &d, &pr);
    for(int i=1;i<=m;++i)
        scanf("%d%d%d%d", &v[i].x, &v[i].y, &v[i].tip, &v[i].cost);
    sort(v+1, v+m+1, cresc);
    for(int i=1;i<=n;++i)
        t[i]=i;
    for(int i=1;i<=m && nr<n-1; ++i)
    {
        if(t[father(v[i].x)]!=t[father(v[i].y)])
        {
            p[++nr]=v[i].cost;
            join(v[i].x, v[i].y);
        }
    }
    sort(p+1, p+nr+1, descr);
    p[nr+1]=-1;
    for(int i=1; i<=nr+1 && secv<pr; ++i)
    {
        if(p[i]!=p[i-1]){
            printf("%d\n", p[i]);
            secv++;
        }
    }
    return 0;
}