Cod sursa(job #1201110)

Utilizator robertstrecheStreche Robert robertstreche Data 24 iunie 2014 14:50:02
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <vector>
#include <bitset>

#define lmax 100005

using namespace std;

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

vector < pair<int,int> > v[lmax];

int n,m,x,y,a,b,c;

inline void read()
{
     f>>n>>m>>x>>y;

    for (int i=1;i<=m;i++)
     {
         f>>a>>b>>c;

         v[a].push_back(make_pair(b,c));
         v[b].push_back(make_pair(a,c));
     }

}

inline bool solve()
{
    int p=1,u=1;
    int l[lmax];
    int drum[lmax];

    bitset <lmax>ap;

    l[1]=x;

    drum[x]=0;

    while (p<=u)
     {
         int val=l[p];
         ap[val]=1;
         p++;

         for (vector<pair<int,int> >::iterator i=v[val].begin();i!=v[val].end();i++)
          if (!ap[i->first])
           {
               if (i->first>val)
                drum[i->first]=drum[val]+i->second;
               else
                 drum[i->first]=drum[val]-i->second;

               u++;
               l[u]=i->first;
           }
          if (drum[y])
           {   if (drum[y]>0)
                g<<drum[y];
               else
                g<<-drum[y];

               return 1;
           }
     }
}

int main()
{
    read();

    solve();

   f.close();
   g.close();
}