Cod sursa(job #525926)

Utilizator ucc_5Usurelu Catalin ucc_5 Data 26 ianuarie 2011 19:06:12
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 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,dist[nmax];
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;
  memset (inq,false,sizeof (inq));
  
  q.push (x);
  inq[x]=1;
  
  while (true)
  {
    varf=q.front ();
    q.pop ();
    for (i=0; i<g[varf].size (); i++)
      if (inq[g[varf][i].y]==false)
        {
          inq[g[varf][i].y]=1;
          q.push (g[varf][i].y);
          dist[g[varf][i].y]=dist[varf]+g[varf][i].cost;
          if (g[varf][i].y==Y)
            return dist[Y];
        }
  }
}

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