Pagini recente » Cod sursa (job #1864111) | Cod sursa (job #2197014) | Cod sursa (job #2338741) | Cod sursa (job #968891) | Cod sursa (job #525926)
Cod sursa(job #525926)
#include <fstream>
#include <vector>
#include <queue>
#define nmax 30002
using namespace std;
ifstream f("sate.in");
ofstream g1("sate.out");
int X,Y,n,m,i,dist[nmax];
struct nod { int y,cost; } aux1,aux2;
vector <nod> g[nmax];
void citire ()
{
int i,x,y,cost;
f>>n>>m>>X>>Y;
for (i=1; i<=m; i++)
{
f>>x>>y>>cost;
aux1.y=y; aux1.cost=cost;
g[x].push_back (aux1);
aux2.y=x; aux2.cost=-cost;
g[y].push_back (aux2);
}
f.close ();
}
int bfs (int x)
{
queue <int> q;
bool inq[nmax];
int varf;
memset (inq,false,sizeof (inq));
q.push (x);
inq[x]=1;
while (true)
{
varf=q.front ();
q.pop ();
for (i=0; i<g[varf].size (); i++)
if (inq[g[varf][i].y]==false)
{
inq[g[varf][i].y]=1;
q.push (g[varf][i].y);
dist[g[varf][i].y]=dist[varf]+g[varf][i].cost;
if (g[varf][i].y==Y)
return dist[Y];
}
}
}
void afisare ()
{
g1<<bfs(X);
g1.close ();
}
int main ()
{
citire ();
afisare ();
return 0;
}