Pagini recente » Cod sursa (job #334593) | Cod sursa (job #1207020) | Cod sursa (job #3253959) | Cod sursa (job #2248087) | Cod sursa (job #1040306)
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int Nmax = 30002;
vector<int> G[Nmax],C[Nmax];
int D[Nmax],Piv[Nmax];
queue<int> q;
int inq[Nmax];
int N,M,X,Y;
const int buffsize = 1<<16;
char buff[buffsize];
int wh;
int isdigit(char c){
return ('0'<=c && c<='9');
}
void Read(int &x){
while(!isdigit(buff[wh])){
wh++;
if(wh==buffsize){
in.read(buff,buffsize);
wh=0;
}
}
x=0;
while(isdigit(buff[wh])){
x= x*10 + int(buff[wh])-'0';
wh++;
if(wh==buffsize){
in.read(buff,buffsize);
wh=0;
}
}
}
int main(){
Read(N);
Read(M);
Read(X);
Read(Y);
for(int i=1;i<=M;i++){
int x,y,c;
Read(x);
Read(y);
Read(c);
G[x].push_back(y);
G[y].push_back(x);
C[x].push_back(c);
C[y].push_back(c);
}
memset(D,-1,sizeof(D));
int i=1;
while( D[X]==-1 || D[Y]==-1 || i<=N){
if( D[i]==-1 ){
D[i]=0;
Piv[i]=i;
q.push(i);
inq[i]=1;
while(!q.empty()){
int p=q.front();
q.pop();
inq[p]=0;
for(int j=0;j<G[p].size();j++){
if( D[ G[p][j] ]==-1 ){
if( G[p][j] < p ){
D[ G[p][j] ] = D[p] - C[p][j];
}
else{
D[ G[p][j] ] = D[p] + C[p][j];
}
Piv[ G[p][j] ] = Piv[p];
if( !inq[ G[p][j] ] ){
q.push(G[p][j]);
inq[G[p][j]]=1;
}
}
}
}
}
i++;
}
out<< D[max(X,Y)] - D[min(X,Y)] ;
return 0;
}