Cod sursa(job #1708299)

Utilizator Cham3leonMitocaru Andreea Cham3leon Data 26 mai 2016 20:56:07
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("sate.in");
ofstream g("sate.out");

int N,M,X,Y;
int i,j,D;
bool ap[30001];
bool ok=true;
struct Sat
{   int sat,lungime; };

vector<Sat> v[30001];

void BK(int r, int d)
{
    if(ok)
       {
           if(r==Y)
        {
            g<<d;
            ok=false;
        }
        else
            for(vector<Sat>::iterator aux = v[r].begin();aux!=v[r].end();++aux)
            {
                if(!ap[aux->sat])
                    {ap[aux->sat]=true;
                     BK(aux->sat,d+aux->lungime);}
            }
       }
}
int main()
{   int k;
    f>>N>>M>>X>>Y;

    for(k=1;k<=M;k++)
    {
        f>>i>>j>>D;
        ap[i]=false;
        ap[j]=false;
        Sat s1,s2;
        s1.sat=j;
        s1.lungime=D;
        s2.sat=i;
        s2.lungime=-D;
        v[i].push_back(s1);
        v[j].push_back(s2);
    }
    ap[X]=true;
    BK(X,0);
    f.close();
    g.close();
    return 0;
}