Cod sursa(job #1484454)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 11 septembrie 2015 11:18:29
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include<iostream>
#include<fstream>
#include<queue>

using namespace std;

ifstream fin ("sate.in");
ofstream fout ("sate.out");

struct muchie{
   int nod, cost;
};

vector <muchie> L[100029];
int n,m,x1,x2,lera_lynn;
int cost[30009];
bool viz[30009];

void Citire()
{
  int i,j,x,y,costt;
  muchie w;

  if (x1>x2) swap(x1,x2);
   
  fin >> n >> m >> x1 >> x2;

  for (i=1; i<=m; i++)
 {
    fin >> x >> y >> costt;
    if (x>y) swap(x,y); 

    // daca ma duc din x in y e cost pozitiv
    // daca ma duc din y in x e cost negativ

    w.nod=y;
    w.cost=costt;
    L[x].push_back(w);

    w.nod=x;
    w.cost=costt * (-1);
    L[y].push_back(w);
 }
}

int Rezolva()
{
  queue <int> q;
  int i,j,k,costt,distanta;
  q.push(x1);
  viz[x1]=1;
  
   while (!q.empty())
 {
   k=q.front();
   q.pop();
   
   for (j=0; j<L[k].size(); j++)
  {
       i = L[k][j].nod; costt=L[k][j].cost;
       if (!viz[i])            
      {
         cost[i]=cost[k]+costt;
         viz[i]=1;
         if (i==x2) 
         {
           distanta=cost[i];
           return distanta;
         }
         q.push(i); 
      }
  }
 }
}

void Afisare()
{
fout << lera_lynn << "\n";
}

int main ()
{
 Citire();
 lera_lynn = Rezolva();
 Afisare();
 fin.close();
 fout.close();
 return 0;
}