Pagini recente » Cod sursa (job #1633198) | Cod sursa (job #2986766) | Cod sursa (job #1394612)
#include <fstream>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int st[300001], N, M, prim, ultim, x1, y1, p, i, x, y;
struct nod{
int val,pret;
nod *urm;
} *v[30001], *t;
struct coada {
int n,p;
};
coada viz[30001];
void add(int x1,int y1,int p){
nod *t;
t=new nod;
t->val=y1;
t->pret=p;
t->urm=v[x1];
v[x1]=t;
t=new nod;
t->val=x1;
t->pret=p;
t->urm=v[y1];
v[y1]=t;
}
void citire(int &N, int &M, int &x, int &y){
f >> N >> M >> x >> y;
for (i = 1;i <= M; i++){
f >> x1 >> y1 >> p;
add (x1,y1,p);
}
}
int main()
{
citire( N, M, x, y);
viz[x].n = 1;
prim = 1;
ultim = 1;
st[prim] = x;
while(!viz[y].n){
for (t = v[st[prim]]; t; t=t->urm)
if (!viz[t->val].n){
viz[t->val].n = 1;
if (t->val > st[prim])
viz[t->val].p = viz[st[prim]].p + t->pret;
else
viz[t->val].p = viz[st[prim]].p - t->pret;
st[++ultim] = t->val;
if (t->val == y)
break;
}
prim++;
}
g << viz[y].p;
}