Pagini recente » Statisticile problemei Cartonase | Cod sursa (job #648399) | Cod sursa (job #71382) | Cod sursa (job #2425864) | Cod sursa (job #2970168)
#include <fstream>
#include <queue>
#include <bitset>
#include <vector>
using namespace std;
string file = "sate";
ifstream cin(file + ".in");
ofstream cout(file + ".out");
vector <int> distanta;
struct sat {
int nod;
int distanta;
};
vector <sat> L[30001];
void bfs(int x)
{
queue <int> Q;
Q.push(x);
distanta[x] = 0;
while (!Q.empty())
{
x = Q.front();
Q.pop();
for (auto y : L[x])
{
if (distanta[y.nod] == -1)
{
Q.push(y.nod);
distanta[y.nod] = distanta[x] + y.distanta;
}
}
}
}
int main()
{
int n, m, x, y, a,b,d;
cin >> n >> m >> x >> y;
distanta.resize(n + 1, -1);
for (int i = 1; i <= m; i++)
{
cin >> a >> b >> d;
L[a].push_back({b,d});
L[b].push_back({a,-d});
}
bfs(x);
cout << distanta[y];
}