Cod sursa(job #1028518)

Utilizator macajouMaca George macajou Data 14 noiembrie 2013 11:31:23
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdio>
#define nmax 15010
#define infi 1<<30

using namespace std;

struct nod
{
    int inf;
    nod *urm;
};

nod *l[nmax];
int n,m,k,viz[nmax],cost[nmax][nmax];


int maxim(int a, int b)
{
    if(a>b)
       return a;
    return b;
}

int minim(int a, int b)
{
    if(a<b)
       return a;
    return b;
}

void adaug(nod *&pr, int x)
{
    nod *nou;
    nou=new nod;
    nou->inf=x;
    nou->urm=pr;
    pr=nou;
}

void citire()
{
    int i,x,y,c;
    scanf("%d%d%d",&n,&m,&k);
    for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            cost[x][y]=cost[y][x]=c;
            adaug(l[x],y);
            adaug(l[y],x);
        }
}

int drum(int st, int fin,int max)
{
    int x,m;
    nod *c;
    if(cost[st][fin]==0)
       cost[st][fin]=infi;
    for(c=l[st];c;c=c->urm)
        if(c->inf!=fin && !viz[c->inf])
           {
               viz[c->inf]=1;
               m=maxim(cost[st][c->inf],max);
               x=drum(c->inf,fin,m);
               cost[st][fin]=minim(cost[st][fin],x);
               viz[c->inf]=0;
           }
    return maxim(max,cost[st][fin]);
}

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

    citire();
    int x,y;
    for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&x,&y);
            viz[y]=1;
            printf("%d\n",drum(y,x,0));
            viz[y]=0;
        }

    return 0;
}