Pagini recente » Cod sursa (job #663541) | Cod sursa (job #807571) | Cod sursa (job #249836) | Cod sursa (job #895233) | Cod sursa (job #2680749)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
vector <pair<int,int>> graf[30001];
stack <pair<int,int>> ms;
int n,m,st,fn,vf[30001],dist;
void citire()
{
in>>n>>m>>st>>fn;
for(int i=1;i<=m;i++)
{
int x,y,z;
in>>x>>y>>z;
graf[x].push_back({y,z});
graf[y].push_back({x,z});
}
}
void dfs()
{
ms.push({st,0});
vf[st]=1;
while(!ms.empty())
{
int nod=ms.top().first;
ms.pop();
int nr=graf[nod].size();
for(int i=0;i<nr;i++)
{
int nextnod=graf[nod][i].first,drum=graf[nod][i].second;
if(vf[nextnod]==0)
{
vf[nextnod]=1;
if(nextnod<nod)
dist-=drum;
else
dist+=drum;
ms.push({nextnod,drum});
}
}
}
}
int main()
{
citire();
dfs();
out<<dist;
return 0;
}