Pagini recente » Cod sursa (job #2218988) | Cod sursa (job #2189035) | Cod sursa (job #2597) | Cod sursa (job #70372) | Cod sursa (job #2839268)
#include <bits/stdc++.h>
#define Dmax 30005
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector < pair < int,int > >G[Dmax];
pair < int, int > tati[Dmax];
int n,m,start,stop;
bool viz[Dmax];
queue < int > Q;
void BFS(int start)
{
Q.push(start);
tati[start].first = 0;
tati[start].second = 0;
viz[start] = true;
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(unsigned int i = 0; i < G[nod].size(); i++)
{
int Vecin = G[nod][i].first;
if(!viz[Vecin])
{
viz[Vecin] = true;
tati[Vecin].first = nod;
tati[Vecin].second = G[nod][i].second;
Q.push(Vecin);
}
}
}
}
int main()
{
f>>n>>m>>start>>stop;
for(int i = 1; i <= m; i++)
{
int x,y,z;
f>>x>>y>>z;
G[x].push_back({y,z});
G[y].push_back({x,z});
}
BFS(start);
int y = stop;
int sum = 0;
while(y!=start)
{
if(y > tati[y].first)
sum += tati[y].second;
else
sum -= tati[y].second;
y = tati[y].first;
}
g<<sum;
return 0;
}