Cod sursa(job #664249)

Utilizator rootsroots1 roots Data 19 ianuarie 2012 20:43:14
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.81 kb
#include <fstream>
#include <cstring>

#define INF 251

#define gL 201
#define useL 201
#define TL 201
#define dL 201

using namespace std;

ifstream in;
ofstream out;

struct graf
{
    unsigned char nod,cost;
    graf *link;
}*g[gL];

unsigned char d[dL],use[useL],T[TL];
unsigned char N,sol=0;

inline void addedge(unsigned char x,unsigned char y,unsigned char c)
{
    graf *p=new graf;
    p->nod=y;
    p->cost=c;
    p->link=g[x];
    g[x]=p;
}

inline void Prim()
{
    unsigned char nod;

    d[1]=0;
    for(unsigned char i=2;i<=N;++i) d[i]=INF;
    memset(T,0,sizeof(T));
    memset(use,0,sizeof(use));

    for(unsigned char min,ok;1;)
    {
        ok=1;
        min=INF;
        for(unsigned char i=1;i<=N;++i)
            if(!use[i]&&d[i]<min)
            {
                min=d[i];
                ok=0;
                nod=i;
            }

        if(ok) return;

        for(graf *p=g[nod];p;p=p->link)
            if(d[p->nod]>p->cost)
            {
                d[p->nod]=p->cost;
                T[p->nod]=nod;
            }

        sol+=d[nod];
        use[nod]=1;
    }
}

int main()
{
    unsigned char M,K,x,y,c,b;
    in.open("online.in");

    in>>N>>M;
    for(;M--;)
    {
        in>>x>>y>>c;
        addedge(x,y,c);
        addedge(y,x,c);
    }

    Prim();

    out.open("online.out");

    out<<sol<<'\n';

    unsigned char pos,max=0;
    unsigned char auxt,auxc,ax,bx;

    in>>K;
    for(;K--;)
    {
        in>>x>>y>>c;

        if(T[x]==y&&d[x]>c)
        {
            sol+=c-d[x];
            d[x]=c;
        }
        else
        if(T[y]==x&&d[y]>c)
        {
            sol+=c-d[y];
            d[y]=c;
        }
        else
        if(T[x]!=y&&T[y]!=x)
        {
            unsigned char end=1;
            memset(use,0,sizeof(use));
            for(unsigned char nod=x;nod!=1;nod=T[nod])
                use[nod]=1;
            for(unsigned char nod=y;nod!=1;nod=T[nod])
                if(use[nod])
                {
                    end=nod;
                    break;
                }

            max=0;
            pos=0;
            b=0;
            for(unsigned char nod=x;nod!=end;nod=T[nod])
                if(d[nod]>max)
                {
                    max=d[nod];
                    pos=nod;
                    b=1;
                }

            for(unsigned char nod=y;nod!=end;nod=T[nod])
                if(d[nod]>max)
                {
                    max=d[nod];
                    pos=nod;
                    b=2;
                }

            if(max>c)
            {
                sol+=c;
                sol-=max;
                if(b==1)
                {
                    auxt=y;
                    auxc=c;
                    for(unsigned char nod=x;nod!=pos;nod=T[nod])
                    {
                        ax=T[nod];
                        bx=d[nod];
                        T[nod]=auxt;
                        d[nod]=auxc;
                        auxt=ax;
                        auxc=bx;
                    }
                    T[pos]=auxt;
                    d[pos]=auxc;
                }
                else
                {
                    auxt=x;
                    auxc=c;
                    for(unsigned char nod=y;nod!=pos;nod=T[nod])
                    {
                        ax=T[nod];
                        bx=d[nod];
                        T[nod]=auxt;
                        d[nod]=auxc;
                        auxt=ax;
                        auxc=bx;
                    }
                    T[pos]=auxt;
                    d[pos]=auxc;
                }
            }
        }

        out<<sol<<'\n';
    }

    in.close();
    out.close();

    return 0;
}