Pagini recente » Cod sursa (job #1043126) | Cod sursa (job #1051669) | Cod sursa (job #478824) | Cod sursa (job #1695456) | Cod sursa (job #1769341)
#include <iostream>
#include<fstream>
#include<vector>
#include<fstream>
#include<algorithm>
#include<cmath>
#define Nmax 20000002
typedef int coada[30001];
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int a,p,cst[30001],n,m,x,y,i,j,cost,q,k,l,nod,ultim;
coada c;
bool ok;
vector<pair <int,int> >v[30001];
inline void BFS(int nod)
{
int k,l,n1=n*n;
cst[nod]=0;
int siz=1,start=1;
c[start]=nod;
while(siz>=start)
{
k=c[start];
for(int i=0;i<v[k].size();i++)
{
l=v[k][i].first;
{
if(cst[l]>cst[k]+v[k][i].second)
{
cst[l]=cst[k]+v[k][i].second;
c[++siz]=l;
}
}
}
start++;
}
}
int main()
{
f>>n>>m>>x>>y;
for(q=1;q<=m;q++)
{
f>>i>>j>>cost;
if(i>j)
swap(i,j);
v[i].push_back(make_pair(j,cost));
v[j].push_back(make_pair(i,-cost));
}
for(i=1;i<=n;i++)
cst[i]=Nmax;
BFS(x);
g<<cst[y];
return 0;
}