Pagini recente » Cod sursa (job #1132597) | Cod sursa (job #3129038) | Cod sursa (job #1525049) | Cod sursa (job #1692221) | Cod sursa (job #628906)
Cod sursa(job #628906)
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#define max_n 30001
#define nod first
#define cost second
using namespace std;
int n,m,i,x,y,z,d1,s;
vector < pair < int,int > > g[max_n];
vector < pair < int,int > >::iterator it;
int q[max_n];
int d[max_n];
bool ok[max_n];
int BF() {
int st,dr;
st=dr=1;
q[1]=s;
d[s]=0;
ok[s]=true;
memset(ok,false,sizeof(ok));
for (;st<=dr; st++) {
x=q[st];
for (it=g[x].begin(); it!=g[x].end(); it++) {
y=(*it).nod;
if (!ok[y]) {
ok[y]=true;
q[++dr]=y;
d[y]=d[x]+(*it).cost;
}
if (y==d1) return d[y];
}
}
}
int main () {
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&s,&d1);
for (i=1; i<=m; i++) {
scanf("%d%d%d",&x,&y,&z);
g[x].push_back(make_pair(y,z));
g[y].push_back(make_pair(x,-z));
}
printf("%d\n",BF());
return 0;
}