Cod sursa(job #1020558)

Utilizator evodaniVasile Daniel evodani Data 2 noiembrie 2013 11:08:15
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <cstdio>
#include <vector>
#include <queue>
#define NMAX 30005
using namespace std;
FILE *fin, *fout;
struct varf {
    short int vf;
    int c;
}aux;
vector <varf> graf[NMAX];
queue <short int> coada;
long dist[NMAX];
int main()
{
    int n, m, i, xx, yy, lg;
    short int vf1, vf2;
    bool ready=0;
    fin=fopen("sate.in", "r"); fout=fopen("sate.out", "w");
    fscanf(fin, "%d%d%d%d", &n, &m, &xx, &yy);

    for (i=1; i<=m; ++i) {
        fscanf (fin, "%hd%hd%d", &vf1, &vf2, &aux.c);
        aux.vf=vf1; graf[vf2].push_back(aux);
        aux.vf=vf2; graf[vf1].push_back(aux);
    }

    //BFS
    coada.push(xx); dist[xx]=1;
    while (!coada.empty() && !ready) {
        vf1=coada.front(); coada.pop();
        lg=graf[vf1].size();

        for (i=0; i<lg; ++i) if (!dist[graf[vf1][i].vf]) {
            vf2=graf[vf1][i].vf;

            if (vf2>vf1)
                dist[vf2]=dist[vf1]+graf[vf1][i].c;
            else
                dist[vf2]=dist[vf1]-graf[vf1][i].c;

            coada.push(graf[vf1][i].vf);

            if (graf[vf1][i].vf==yy) {
                ready=1;
                break;
            }
        }
    }

    fprintf(fout, "%ld\n", dist[yy]-1);
    fclose(fin); fclose(fout);
    return 0;
}