Cod sursa(job #525925)

Utilizator ucc_5Usurelu Catalin ucc_5 Data 26 ianuarie 2011 19:05:30
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <vector>
#include <queue>
#define nmax 30002
using namespace std;
ifstream f("sate.in");
ofstream g1("sate.out");

int X,Y,n,m,i;
struct nod { int y,cost; } aux1,aux2;

vector <nod> g[nmax];


void citire ()
{
  int i,x,y,cost;
  f>>n>>m>>X>>Y;
  for (i=1; i<=m; i++)
  {
    f>>x>>y>>cost;
    aux1.y=y; aux1.cost=cost;
    g[x].push_back (aux1);
    aux2.y=x; aux2.cost=-cost;
    g[y].push_back (aux2);
  }
  f.close ();
}

int bfs (int x)
{
  queue <int> q;
  bool inq[nmax];
  int varf=x,dist=0; //nu ne trebuie vector pentru dist ca ne intereseaza doar distanta anterioara
  memset (inq,false,sizeof (inq));

  inq[x]=1;
  
  while (true)
  {
    for (i=0; i<g[varf].size (); i++)
      if (inq[g[varf][i].y]==false)
        {
          inq[g[varf][i].y]=1;
          dist=dist+g[varf][i].cost;
          if (g[varf][i].y==Y)
            return dist;
          
          varf=g[varf][i].y; // nu e nevoie de coada, ca avem doar un varf in ea mereu
        }
  }
}

void afisare ()
{
  g1<<bfs(X);
  g1.close ();
}
  
int main ()
{
  citire ();
  afisare ();
  return 0;
}