Pagini recente » Cod sursa (job #2919272) | Cod sursa (job #303123) | Cod sursa (job #2640822) | Cod sursa (job #2734350) | Cod sursa (job #1803215)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f ("sate.in");
ofstream t ("sate.out");
struct edges{
int dest,cost;};
vector < vector <edges> > v(30010);
int n,m;
bool vaz[30010];
int bfs(int nod,int target){int curent,cost=0;
queue <int> q;
q.push(nod);
while (!q.empty()){
start:
curent=q.front();
vaz[curent]=true;
if (curent==target) return cost;
q.pop();
for (unsigned i=0;i<v[curent].size();++i)
if (!vaz[v[curent][i].dest]){
q.push(v[curent][i].dest);
if (v[curent][i].dest>curent) cost+=v[curent][i].cost;
else cost-=v[curent][i].cost;
goto start;
}
}
return 0;
}
int main()
{
int x,y,t1,t2,c;
f>>n>>m>>x>>y;
for (int i=0;i<m;++i)
f>>t1>>t2>>c,v[t1].push_back({t2,c}),v[t2].push_back({t1,c});
t<<bfs(x,y);
return 0;
}