Cod sursa(job #1216894)

Utilizator cojocarugabiReality cojocarugabi Data 6 august 2014 00:56:09
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <iostream>
#define nmax 30005
using namespace std;
typedef struct node{int x;int y;node *next;} *nod;
nod S[nmax];
int D[nmax],C[nmax];
int main(void)
{
    ifstream fi("sate.in");
    ofstream fo("sate.out");
    int x,y,n,m;
    for (fi>>n>>m>>x>>y;m;--m)
    {
        int X,Y,cost;
        fi>>X>>Y>>cost;
        nod p;
        p=new node;
        p->next=S[X];
        p->x=Y;p->y=cost;
        S[X]=p;
        p=new node;
        p->next=S[Y];
        p->x=X;p->y=-cost;
        S[Y]=p;
    }
    C[1]=x;
    D[x]=0;
    int g=1,a;
    nod p;
    for (int f=1;f<=g;++f)
        for (a=C[f],p=S[a];p!=NULL;p=p->next)
                    if (!D[p->x] && p->x!=x)
                          D[p->x]=D[a]+p->y,C[++g]=p->x;
    fo<<D[y]<<"\n";
}