Pagini recente » Cod sursa (job #2394704) | Cod sursa (job #1587371) | Cod sursa (job #1193763) | Cod sursa (job #2251720) | Cod sursa (job #1118911)
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
fstream f("sate.in",ios::in);
fstream g("sate.out",ios::out);
const int nmax=30005,INF=0x3f3f3f3f;
vector < pair <int,int> > a[nmax];
int n,m,x,y,x1,y11,c,aux,i;
void citire()
{
f>>n>>m>>x>>y;
if(x>y)
{
aux=x;
x=y;
y=aux;
}
for(i=1;i<=m;i++)
{
f>>x1>>y11>>c;
a[x1].push_back(make_pair(y11,c));
a[y11].push_back(make_pair(x1,c));
}
}
void rezolvare()
{
long long dist[nmax];
int viz[nmax],nc;
queue <int> q;
memset(dist,INF,sizeof(dist));
memset(viz,0,sizeof(viz));
q.push(x);
dist[x]=0;
nc=x;
viz[x]=1;
while (!q.empty())
{
nc=q.front();
q.pop();
for (vector <pair <int,int> >::iterator it=a[nc].begin();it!=a[nc].end();++it)
if (!viz[it->first])
{
if (nc<it->first) dist[it->first]=dist[nc]+it->second;
else dist[it->first]=dist[nc]-it->second;
viz[it->first]=1;
q.push(it->first);
}
}
g<<dist[y];
}
int main()
{
citire();
rezolvare();
f.close();g.close();
return 0;
}