Cod sursă (job #349093)

Utilizator avatar Horiss Horia Banciu Horiss IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp | 2,76 kb
Rundă Arhiva de probleme Status evaluat
Dată 6 feb. 2018 00:02:44 Scor 100
#include<cstdio>
#include<algorithm>
#define MAX_N 10000
#define MAX_M 100000
using namespace std;

struct edge {
    int x, y, cost;
};

struct node {
    int val, cost;
    node* next;
};

edge v[MAX_M];
node* apm[MAX_N + 1];
int p[MAX_N + 1], h[MAX_N + 1], n, m, k, minCost, nrm;

inline bool cmp(edge a, edge b) {
    return a.cost < b.cost;
}

inline void insertEdge(int x, int y, int cost) {
    node* elem = new node;
    elem->val = y;
    elem->cost = cost;
    elem->next = apm[x];
    apm[x] = elem;
}

inline void deleteEdge(int x, int y) {
    node* p, *q;
    p = apm[x];
    if(p->val == y) {
        apm[x] = apm[x]->next;
        delete p;
    } else {
        while(p->next->val != y)
            p = p->next;
        q = p->next;
        p->next = p->next->next;
        delete q;
    }
}

inline void Init(int n) {
    for(int i = 1; i <= n; i++) {
        p[i] = i;
        h[i] = 1;
    }
}

inline int Find(int x) {
    int r, tmp;
    r = x;
    while(p[r] != r)
        r = p[r];
    while(p[x] != r) {
        tmp = p[x];
        p[x] = r;
        x = tmp;
    }
    return r;
}

inline void Union(int x, int y, int i) {
    int rx, ry;
    rx = Find(x);
    ry = Find(y);
    if(rx != ry) {
        minCost += v[i].cost;
        insertEdge(x,y,v[i].cost);
        insertEdge(y,x,v[i].cost);
        nrm++;
        if(h[rx] < h[ry])
            p[rx] = ry;
        else if(h[rx] > h[ry])
            p[ry] = rx;
        else {
            p[rx] = ry;
            h[ry]++;
        }
    }
}

int traverse(int x, int from, int target, int &u1, int &v1, int &largestCost) {
    if(x == target) {
        largestCost = 0;
        return 1;
    }
    for(node* p = apm[x]; p != NULL; p = p->next) {
        if(p->val != from && traverse(p->val,x,target,u1,v1,largestCost)) {
            if(p->cost > largestCost) {
                largestCost = p->cost;
                u1 = x;
                v1 = p->val;
            }
            return 1;
        }
    }
    return 0;
}

int main() {
    int i, x, y, cost, u1, v1, largestCost;
    FILE *fin, *fout;
    fin = fopen("zapada.in","r");
    fout = fopen("zapada.out","w");
    fscanf(fin,"%d%d%d",&n,&m,&k);
    for(i = 0; i < m; i++)
        fscanf(fin,"%d%d%d",&v[i].x,&v[i].y,&v[i].cost);
    sort(v,v+m,cmp);
    Init(n);
    for(i = 0; nrm < n - 1; i++)
        Union(v[i].x,v[i].y,i);
    fprintf(fout,"%d\n",minCost);
    for(i = 0; i < k; i++) {
        fscanf(fin,"%d%d%d",&x,&y,&cost);
        traverse(x,0,y,u1,v1,largestCost);
        if(largestCost > cost) {
            deleteEdge(u1,v1);
            deleteEdge(v1,u1);
            insertEdge(x,y,cost);
            insertEdge(y,x,cost);
            minCost += cost - largestCost;
        }
        fprintf(fout,"%d\n",minCost);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}