Cod sursa(job #1112768)

Utilizator smaraldaSmaranda Dinu smaralda Data 19 februarie 2014 23:41:02
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;

const int KMAX = 1000, NMAX = 25e4, INF = 2e9;

struct EDGE {
    int node, k;
    EDGE (int node = 0, int k = 0) {
        this->node = node;
        this->k = k;
    }
};

vector <EDGE> graph[NMAX + 5];
vector <int> cost[NMAX + 5];
int n, c, m, start, fin, dmin[NMAX + 5];

void read() {
    char ln[20];
    int j, i, len, x, y, k;

    scanf("%d%d%d%d\n",&n,&m,&start,&fin);
    for(i = 1; i <= m; i++) {
        gets(ln);

        len = strlen(ln);
        x = 0;
        for(j = 0; j < len; j++)
            if(ln[j] >= '0' && ln[j] <= '9')
                x = x * 10 + ln[j] - '0';
            else
                break;

        y = 0;
        for(++j; j < len; ++j)
            if(ln[j] >= '0' && ln[j] <= '9')
                y = y * 10 + ln[j] - '0';
            else
                break;

        k = 0;
        for(++j; j < len; ++j)
            if(ln[j] >= '0' && ln[j] <= '9')
                k = k * 10 + ln[j] - '0';
            else
                break;
        graph[x].push_back(EDGE(y,k));
    }
}

void expand (int node) {
    vector <EDGE>::iterator it;

    for(it = graph[node].begin(); it != graph[node].end(); ++it)
        if(max(dmin[node], it->k) < dmin[it->node]) {
            dmin[it->node] = max(dmin[node], it->k);
            cost[max(dmin[node], it->k)].push_back(it->node);
        }
}

void dijkstra () {
    int node, i;

    for(node = 1; node <= n; node++)
        dmin[node] = INF;

    cost[0].push_back(start);
    dmin[start] = 0;

    for(c = 0; c <= 1000; c++) {
        for(i = 0; i < cost[c].size(); i++) {
            if(c > dmin[ cost[c][i] ])
                continue;

            if(cost[c][i] == fin)
                return;
            expand(cost[c][i]);
        }

        cost[c].clear();
    }
}


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


    read();

    dijkstra();

    printf("%d\n",dmin[fin]);
    return 0;
}