Cod sursa(job #6881)

Utilizator StTwisterKerekes Felix StTwister Data 21 ianuarie 2007 10:25:23
Problema Radiatie Scor 30
Compilator cpp Status done
Runda preONI 2007, Runda 1, Clasele 11-12 Marime 1.65 kb
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

#define NMAX 15001
#define MMAX 30001
#define INF 0x3f3f3f3f

struct Nod
{
    int inf;
    int c;
    Nod* urm;
};

typedef Nod* PNod;

PNod G[NMAX];
int m[NMAX];
int N, M, K;

using namespace std;

inline int min(int a, int b)
{
    return a<b ? a : b;
}

inline int max(int a, int b)
{
    return a>b ? a : b;
}

int dfs(int x)
{
    int li, ls;
    memset(m, 0x3f, sizeof(m));
    queue<int> Q;
    Q.push(x);
    m[x] = 0;
    while (!Q.empty())
    {
        int p = Q.front();
        Q.pop();
        PNod nod = G[p];
        while (nod != NULL)
        {
            int f = m[nod->inf];
            m[nod->inf] = min(m[nod->inf], max(m[p], nod->c));
            if (m[nod->inf] != f)
                Q.push(nod->inf);
            nod = nod->urm;   
        }
    }
}

int main()
{
    freopen("radiatie.in", "r", stdin);
    freopen("radiatie.out", "w", stdout);
    
    scanf("%d %d %d", &N, &M, &K);
    
    memset(G, 0, sizeof(G));
    
    for (int i = 1; i<=M; ++i)
    {
        int x, y, c;
        scanf("%d %d %d", &x, &y, &c);
        PNod nod = new Nod;
        nod->inf = y;
        nod->c = c;
        nod->urm = G[x];
        G[x] = nod;
    
        PNod nod2 = new Nod;
        nod2->inf = x;
        nod2->c = c;
        nod2->urm = G[y];
        G[y] = nod2;
    }
    
    int last = -1;
    for (int i = 0; i<K; ++i)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        if (x != last)
            dfs(x);
        last = x;
        printf("%d\n", m[y]);
    }
}