Pagini recente » Cod sursa (job #713381) | Cod sursa (job #188021) | Cod sursa (job #1402687) | Cod sursa (job #363555) | Cod sursa (job #1824464)
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#define M 100056
#define MAXBUF (1<<20)
FILE*fi,*fout;
using namespace std;
vector <int> A[M];
vector <long> cost[M];
queue<int> coada;
int n,m,i,j,x,y,el,G[M];
bool viz[M];
long costf[M];
char buf[MAXBUF];
int pos=MAXBUF;
inline char nextch(){
if(pos==MAXBUF){
fread(buf,1,MAXBUF,fi);
pos=0;
}
return buf[pos++];
}
inline int getnr(){
char a=nextch();
while(a<'0'||a>'9')
a=nextch();
int nr=0;
while(a>='0'&&a<='9'){
nr=nr*10+a-'0';
a=nextch();
}
return nr;
}
inline void lee(int x)
{
coada.push(x);
viz[x]=1;
while(!coada.empty())
{
el=coada.front();
coada.pop();
for(int k=0;k<A[el].size();k++)
{
if(viz[A[el][k]]==0)
{
costf[A[el][k]]=costf[A[el][k]]+costf[el]+cost[el][k];
if(A[el][k]==y)
return;
coada.push(A[el][k]);
viz[A[el][k]]=1;
}
}
}
}
int main()
{
int x1,y1,cos;
fi=fopen("sate.in","r");
fout=fopen("sate.out","w");
n=getnr();
m=getnr();
x=getnr();
y=getnr();
while(m!=0)
{
x1=getnr();
y1=getnr();
cos=getnr();
A[x1].push_back(y1);
A[y1].push_back(x1);
cost[x1].push_back(cos);
cost[y1].push_back((-1)*cos);
m--;
}
lee(x);
fprintf(fout,"%d",costf[y]);
return 0;
}