Pagini recente » Cod sursa (job #3213764) | Cod sursa (job #712010) | Cod sursa (job #2550863) | Cod sursa (job #2946163) | Cod sursa (job #1760227)
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
queue <int> Q;
vector <pair <int,int> > G[30005];
int n,m,x,y,i,xx,yy,dist[30005],crt,distance,d,s;
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d %d %d %d",&n,&m,&x,&y);
for (i=1; i<=m; i++)
{
scanf("%d %d %d",&xx,&yy,&d);
G[xx].push_back(make_pair(yy,d));
G[yy].push_back(make_pair(xx,d));
s=x;
}
for (i=1; i<=n; i++) dist[i]=-1;
dist[s]=0;
Q.push(s);
while (!Q.empty())
{
crt=Q.front();
Q.pop();
for (i=0; i<G[crt].size(); i++)
{
if (dist[G[crt][i].first]==-1)
{
Q.push(G[crt][i].first);
if (G[crt][i].first<s && s<=crt) dist[G[crt][i].first]=G[crt][i].second-dist[crt];
if (G[crt][i].first<crt && crt<=s) dist[G[crt][i].first]=G[crt][i].second+dist[crt];
if (s<G[crt][i].first && G[crt][i].first<crt) dist[G[crt][i].first]=dist[crt]-G[crt][i].second;
if (crt<G[crt][i].first && G[crt][i].first<s) dist[G[crt][i].first]=dist[crt]-G[crt][i].second;
if (s<=crt && crt<G[crt][i].first) dist[G[crt][i].first]=G[crt][i].second+dist[crt];
if (crt<=s && s<G[crt][i].first) dist[G[crt][i].first]=G[crt][i].second-dist[crt];
}
}
}
if (s<=x && s<=y) {printf("%d\n",dist[y]-dist[x]); return 0;}
if (s<=y && y<=x) {printf("%d\n",dist[x]-dist[y]); return 0;}
if (x<=s && s<=y) {printf("%d\n",dist[x]+dist[y]); return 0;}
if (y<=s && s<=x) {printf("%d\n",dist[x]+dist[y]); return 0;}
if (x<=y && y<=s) {printf("%d\n",dist[x]-dist[y]); return 0;}
if (y<=x && x<=s) {printf("%d\n",dist[y]-dist[x]); return 0;}
return 0;
}