Pagini recente » Cod sursa (job #1555768) | Cod sursa (job #1517465) | Cod sursa (job #1253573) | Cod sursa (job #1869466) | Cod sursa (job #1297905)
#include<fstream>
#include<vector>
using namespace std;
int modul(int x){
return x<0?-x:x;
}
int n, m2, x, y, i, a, b, d, p, u, D[100003], m[100003], c[100003];
vector <pair<int, int> > L[100003];
ifstream in("sate.in");
ofstream out("sate.out");
int main(){
in>>n>>m2>>x>>y;
for(i=1; i<=m2; i++){
in>>a>>b>>d;
L[a].push_back(make_pair(b, d));
L[b].push_back(make_pair(a, d));
}
D[x]=0;
c[1]=x;
p=u=1;
while(p<=u){
for(i=0; i<L[c[p]].size(); i++){
if(m[L[c[p]][i].first]==0){
c[++u]=L[c[p]][i].first;
m[L[c[p]][i].first]=1;
}
if(L[c[p]][i].first<c[p])
D[L[c[p]][i].first]=modul(D[c[p]]-L[c[p]][i].second);
else
D[L[c[p]][i].first]=D[c[p]]+L[c[p]][i].second;
}
p++;
}
out<<D[y];
return 0;
}