Pagini recente » Cod sursa (job #78465) | Cod sursa (job #2260458) | Cod sursa (job #1248682) | Cod sursa (job #676804) | Cod sursa (job #1022472)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n,m,x,y,i,dist;
struct nod{int s; int d;
};
vector <nod> v[30002];
int length[30002];
bool viz[30002];
using namespace std;
void citire()
{ int s1,s2;
f>>n>>m>>x>>y;
for(i=1;i<=m;i++)
{
f>>s1>>s2>>dist;
nod na;
na.s=s2;na.d=dist;
v[s1].push_back(na);
na.s=s1;na.d=dist;
v[s2].push_back(na);
}
}
void bfs(int x)
{
viz[x]=true;
queue <int> q;
q.push(x);
while(!q.empty())
{
int fr=q.front();
q.pop();
for(i=0;i<v[fr].size();i++)
{
nod nawd=v[fr][i];
if(!viz[nawd.s])
{
q.push(nawd.s);
viz[nawd.s]=true;
if(fr>nawd.s)
{
length[nawd.s]=length[fr]-nawd.d;
}
else
{
length[nawd.s]=length[fr]+nawd.d;
}
}
}
}
}
int main()
{ citire();
bfs(x);
g<<length[y];
return 0;
}