Cod sursa(job #1510177)

Utilizator asavoaeigeoAsavoaei Georgiana asavoaeigeo Data 24 octombrie 2015 17:24:11
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <cstdio>
 
using namespace std;
 
FILE *f=fopen("sate.in", "r");
FILE *g=fopen("sate.out", "w");
 
const int nmax=30001;
const int oo=(1<<31)-1;
int n, m, x, y, viz[nmax], d[nmax];
int xi, xf;
 
struct nod
{
    int info, cost;
    nod*urm;
}*prim[nmax];
 
void AddNod(int x, int y, int c)
{
    nod *p=new nod;
    p->info=y;
    p->cost=c;
    p->urm=prim[x];
    prim[x]=p;
}
 
void Citire()
{
    int i, D;
    fscanf(f, "%d%d%d%d", &n, &m, &xi, &xf);
    for(i=1; i<=m; i++)
    {
        fscanf(f, "%d%d%d", &x, &y, &D);
        AddNod(x, y, D);
        AddNod(y, x, -D);
    }
}
 
void BFS(int xi)
{
    nod *p;
    int pr, ul, c[nmax], xc;
    c[1]=xi;
    pr=ul=1;
    while(pr<=ul)
    {
        xc=c[pr];
        pr++;
        for(p=prim[xc]; p!=0; p=p->urm)
        {
 
            if(viz[p->info]==0)
            {
                //if(d[p->info]>=d[xc]+p->cost)
                {
                    d[p->info]=d[xc]+p->cost;
                    viz[p->info]=1;
                    ul++;
                    c[ul]=p->info;
                }
            }
        }
    }
}
 
int main()
{
    Citire();
    BFS(xi);
    fprintf(g, "%d", d[xf]);
    fclose(f);
    fclose(g);
    return 0;
}