Pagini recente » Cod sursa (job #912711) | Cod sursa (job #295709) | Cod sursa (job #254404) | Cod sursa (job #2546147) | Cod sursa (job #820323)
Cod sursa(job #820323)
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
struct segment{
int fin,dist;
};
int X,Y,n,m,rez,sate[30001];
vector <segment> v[30001];
queue <int> q;
segment makestruct(int x,int y)
{
segment a;
a.fin=x;a.dist=y;
return a;
}
void bfs()
{
int x,y;
while(!q.empty())
{
int curent=q.front();
q.pop();
if(curent==Y)
{
rez=sate[curent];
return;
}
for(int i=0;i<v[curent].size();i++)
{
x=curent;
y=v[curent][i].fin;
if(x<y)
{
q.push(y);
sate[y]=sate[curent]+v[curent][i].dist;
}
else{
q.push(y);
sate[y]=sate[curent]-v[curent][i].dist;
}
}
}
}
int main()
{
int i,x,y,dist;
in>>n>>m>>X>>Y;
for(i=1;i<=m;i++)
{
in>>x>>y>>dist;
v[x].push_back(makestruct(y,dist));
v[y].push_back(makestruct(x,dist));
}
for(i=0;i<v[X].size();i++)
{
q.push(v[X][i].fin);
sate[v[X][i].fin]=v[X][i].dist;
}
bfs();
out<<rez;
}