Cod sursa(job #1714689)

Utilizator AkerToropu Alexandru Aker Data 9 iunie 2016 08:29:02
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <stdio.h>
#include <list>
#define NRSATE 30001
using namespace std;
 
struct rel {
    long dest;
    long dist;
};
 
char parcurs[NRSATE];
list<rel> sat[NRSATE];
 
long parg_rez=0;
void parg (long x, long y, long cdis) {
      parcurs[x]=1;
//      fprintf(stderr,"parg(x==%ld,y==%ld,cdis==%ld)\n",x,y,cdis);
    if (x==y) { parg_rez=cdis; } 
    else {
          for (list<rel>::iterator it=sat[x].begin(); it!=sat[x].end(); it++) {
            if ((parg_rez==0) && (!parcurs[it->dest])) parg(it->dest,y,cdis+it->dist);
          };
    };
};
int main (void) {
        FILE * fi = fopen("sate.in","rt");
    FILE * fo = fopen("sate.out","wt");
 
    long n,m,x,y;
    fscanf(fi,"%ld %ld %ld %ld",&n,&m,&x,&y);
    for (long i=1; i<=m; i++) {
            long sor,sdest,sdist;
        fscanf(fi,"%ld %ld %ld",&sor,&sdest,&sdist);
        rel tmprf;
        tmprf.dest=sdest;
        tmprf.dist=sdist;
        sat[sor].push_back(tmprf);
        tmprf.dest=sor;
        tmprf.dist=-sdist;
        sat[sdest].push_back(tmprf);
    };
    parg(x,y,0);
    fprintf(fo,"%ld",parg_rez);
 
    fclose(fi); fclose(fo);
    return 0;
};