Pagini recente » Cod sursa (job #3144546) | Cod sursa (job #50264) | Cod sursa (job #1701257) | Cod sursa (job #175658) | Cod sursa (job #801211)
Cod sursa(job #801211)
#include <fstream>
#include <vector>
#include <queue>
#define pb push_back
#define mp make_pair
using namespace std;
ifstream f("sate.in");
ofstream h("sate.out");
vector < pair <int, int> > g[30001];
queue <int> q;
int n,m,a,b,x,y,d,i,t[30001],s,way[30001];
bool sel[30001];
void bf (int x)
{
int nod,p,i,nurm,c;
q.push(x); sel[x]=true;way[x]=0;
while (q.back()!=b)
{
nod=q.back();
for (i=0;i<g[nod].size();i++)
{
nurm=g[nod][i].first;
c=g[nod][i].second;
if ( !sel[nurm])
{
q.push(nurm);
sel[nurm]=true;
way[nurm]=way[nod]+c;
}
}
}
}
int main()
{
vector <int> :: iterator it;
f>>n>>m>>a>>b;
for (i=1;i<=m;i++)
{
f>>x>>y>>d;
g[x].pb(mp(y,d));
g[y].pb(mp(x,-d));
}
bf(a);
h<<way[b]<<'\n';
return 0;
}