Cod sursa(job #541562)

Utilizator DraStiKDragos Oprica DraStiK Data 25 februarie 2011 12:07:33
Problema Lazy Scor 100
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.29 kb
#include <algorithm>
using namespace std;

#define DIM 200005

struct muchie
{
    int x,y,ind;
    long long c1,c2;
};

long long cst_min,prf_max;
int t[DIM],r[DIM];
muchie v[DIM];
int N,M;

struct cmp
{
    bool operator () (const muchie &a,const muchie &b) const
    {
        if (a.c1!=b.c1)
            return a.c1<b.c1;
        return a.c2>b.c2;
    }
};

inline int find (int x)
{
    if (x!=t[x])
        t[x]=find (t[x]);
    return t[x];
}

inline void unite (int x,int y)
{
    if (r[x]<r[y])
        t[x]=y;
    else
        t[y]=x;
    if (r[x]==r[y])
        ++r[x];
}

void read ()
{
    int i;

    scanf ("%d%d",&N,&M);
    for (i=1; i<=M; ++i)
    {
        v[i].ind=i;
        scanf ("%d%d%lld%lld",&v[i].x,&v[i].y,&v[i].c1,&v[i].c2);
    }
}

void solve ()
{
    int i;

    for (i=1; i<=N; ++i)
        t[i]=i;
    sort (v+1,v+M+1,cmp ());

    for (i=1; i<=M; ++i)
        if (find (v[i].x)!=find (v[i].y))
        {
            printf ("%d\n",v[i].ind);
            cst_min+=v[i].c1;
            prf_max+=v[i].c1*v[i].c2;
            unite (find (v[i].x),find (v[i].y));
        }
}

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

    read ();
    solve ();

    return 0;
}