Cod sursa(job #7086)

Utilizator goguGogu Marian gogu Data 21 ianuarie 2007 12:25:00
Problema Radiatie Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda 1, Clasele 11-12 Marime 2.63 kb
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
//#include <time.h>
#define MaxM 31000
#define MaxN 15123

using namespace std;

int n, m, nr, cost=0, num, poz=0;
char bun[MaxN];
int x[MaxM], y[MaxM], c[MaxM], st[MaxN], fn[MaxN], ind[MaxN], h[MaxN];
int t[MaxN], sol[MaxN], st2[MaxN], fn2[MaxN], ind2[MaxN], cmin[MaxN];
long long a[MaxM];
char lin[100];

inline int mul(int x)
{
    if (bun[t[x]]) return t[x];
    int k=x;
    while (x!=t[x]) x=t[x];
    while (t[k]!=x){
          t[k]=x;
          k=t[k];
    }
    return x;
}

void cit(int &x)
{
     while (lin[poz]==' ') poz++;
     x=0;
     while (lin[poz]>='0' && lin[poz]<='9')
           x=x*10+lin[poz++]-'0';
}

void check()
{
     int i,j,k;
     while (poz<num && a[poz] <= cost){
           ind[nr]=ind2[poz];
           st[nr]=st2[poz];
           fn[nr++]=fn2[poz];
           poz++;
     }
     for (i=nr-1; i>=0; i--)
         if (mul(st[i])==mul(fn[i])){
            sol[ind[i]]=cost;
            nr--;
            st[i]=st[nr];
            fn[i]=fn[nr];
            ind[i]=ind[nr];
         }
}

int main()
{
//    double timp = clock();
    freopen("radiatie.in", "r", stdin);
    freopen("radiatie.out", "w", stdout);
    int i,j,k;
    gets(lin); poz=0;
    cit(n); cit(m); cit(nr);
    for (i=0; i<n; i++) cmin[i]=1<<30;
    for (i=0; i<m; i++){
        gets(lin); poz=0;
        cit(x[i]); cit(y[i]); cit(c[i]);
        x[i]--; y[i]--;
        a[i]=(x[i]<<15)+y[i];
        a[i]+=((long long)c[i]<<30);
        if (c[i]<cmin[x[i]]) cmin[x[i]]=c[i];
        if (c[i]<cmin[y[i]]) cmin[y[i]]=c[i];
    }
    sort(a, a+m);
    for (i=0; i<m; i++){
        c[i]=a[i]>>30;
        y[i]=a[i]%(1<<15);
        a[i]/=(1<<15);
        x[i]=a[i]%(1<<15);
    }
    for (i=0; i<nr; i++){
        gets(lin); poz=0;
        cit(st[i]); cit(fn[i]);
        st[i]--; fn[i]--;
        ind[i]=i;
        k=cmin[st[i]];
        if (cmin[fn[i]]>k) k=cmin[fn[i]];
        if (st[i]==fn[i]) k=0;
        a[i]=i+( (long long) k << 15);
    }
    sort(a, a+nr);
    for (i=0; i<nr; i++){
        ind2[i]=a[i]%(1<<15);
        a[i] >>=15;
        st2[i]=st[ind2[i]]; 
        fn2[i]=fn[ind2[i]];
    }
    for (i=0; i<n; i++) t[i]=i, sol[i]=1<<30;
    num=nr;
    nr=0;
    memset(bun, 1, n);
    for (i=0; i<m; i++)
        if (mul(x[i])!=mul(y[i])){
        check();
        bun[mul(x[i])]=0;
        t[mul(x[i])]=mul(y[i]);
        cost=c[i];
    }
    check();
    for (i=0; i<num; i++)
        printf("%d\n", sol[i]);
//    printf("A durat %f secunde.\n", (clock()-timp)/CLOCKS_PER_SEC);
    return 0;
}