Pagini recente » Cod sursa (job #2171033) | Cod sursa (job #1216432) | Cod sursa (job #2589090) | Cod sursa (job #1690678) | Cod sursa (job #1219630)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 30005;
int N, M, X, Y, D[MAXN];
bool Used[MAXN];
queue <int> Q;
struct Graf
{
int S;
int C;
};
vector <Graf> G[MAXN];
void BFS()
{
Q.push(X);
Used[X] = 1;
while (!Q.empty())
{
int node = Q.front(); Q.pop();
if (node == Y)
{
printf("%d\n", D[Y]);
return;
}
for (vector <Graf> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
{
Graf i = *it;
if (!Used[i.S])
{
Q.push(i.S);
Used[i.S] = 1;
D[i.S] = D[node] + i.C;
}
}
}
}
Graf Insert(int s, int c)
{
Graf aux;
aux.S = s;
aux.C = c;
return aux;
}
int main()
{
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
scanf("%d %d %d %d\n", &N, &M, &X, &Y);
for (; M; --M)
{
int a, b, c;
scanf("%d %d %d\n", &a, &b, &c);
G[a].push_back(Insert(b, c));
G[b].push_back(Insert(a, -c));
}
BFS();
return 0;
}