Pagini recente » Cod sursa (job #2379327) | Cod sursa (job #1624313) | Cod sursa (job #2649844) | Cod sursa (job #809831) | Cod sursa (job #1604823)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
#define inf 1000000001
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n,m,p,s;
vector<vector<pair<int,int> > > v;
vector<int> dr;
void citire()
{
f>>n>>m>>p>>s;
v=vector<vector<pair<int,int> > > (n+1);
dr=vector<int>(n+1);
for(int i=1; i<=m; i++)
{
int a,b,c;
f>>a>>b>>c;
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
}
void rezolvare()
{
int pre=p;
queue<int>h;
int ok=1;
h.push(p);
while(!h.empty())
{
int r=h.front();
pre=r;
h.pop();
for(vector<pair<int,int> >::iterator it=v[r].begin(); it!=v[r].end(); it++)
{
int q=it->first;
int w=it->second;
if(dr[q]==0)
{
if(q>p)
{
if(q<pre) dr[q]=dr[pre]-w;
else dr[q]=dr[pre]+w;
}
else
{
dr[q]=dr[pre]-w;
}
h.push(q);
}
if(q==s) ok=0;
}
if(ok==0) break;
}
}
int main()
{
citire();
rezolvare();
g<<abs(dr[s]);
return 0;
}