Pagini recente » Cod sursa (job #2651176) | Cod sursa (job #2557329) | Cod sursa (job #2090653) | Cod sursa (job #480366) | Cod sursa (job #1216894)
#include <fstream>
#include <iostream>
#define nmax 30005
using namespace std;
typedef struct node{int x;int y;node *next;} *nod;
nod S[nmax];
int D[nmax],C[nmax];
int main(void)
{
ifstream fi("sate.in");
ofstream fo("sate.out");
int x,y,n,m;
for (fi>>n>>m>>x>>y;m;--m)
{
int X,Y,cost;
fi>>X>>Y>>cost;
nod p;
p=new node;
p->next=S[X];
p->x=Y;p->y=cost;
S[X]=p;
p=new node;
p->next=S[Y];
p->x=X;p->y=-cost;
S[Y]=p;
}
C[1]=x;
D[x]=0;
int g=1,a;
nod p;
for (int f=1;f<=g;++f)
for (a=C[f],p=S[a];p!=NULL;p=p->next)
if (!D[p->x] && p->x!=x)
D[p->x]=D[a]+p->y,C[++g]=p->x;
fo<<D[y]<<"\n";
}