Pagini recente » Cod sursa (job #2362562) | Cod sursa (job #2695800) | Cod sursa (job #2314020) | Cod sursa (job #154838) | Cod sursa (job #1378005)
#include <cstring>
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
typedef std::vector< std::pair<int, int> >::iterator iter;
const int MAXN = 30005;
const int MAXM = 100030;
std::ifstream f("sate.in");
std::ofstream g("sate.out");
std::vector< std::pair<int, int> > G[MAXN]; int n, m, X, Y;
std::priority_queue< std::pair<int, int> > pq;
std::bitset<MAXN> viz;
int dist[MAXN];
void dijkstra(int node)
{
dist[node] = 0;
pq.push(std::make_pair(0, node));
while (!pq.empty()) {
int nd = pq.top().second;
pq.pop();
if (viz[nd] == true) {
continue;
}
viz[nd] = true;
for (iter it = G[nd].begin(); it != G[nd].end(); it++) {
if (viz[it->first] == true) {
continue;
}
if (dist[it->first] > dist[nd] + it->second) {
dist[it->first] = dist[nd] + it->second;
pq.push(std::make_pair(-dist[it->first], it->first));
}
}
}
}
int main()
{
memset(dist, 0x3f, sizeof(dist));
f >> n >> m >> X >> Y;
for (int i = 1, x, y, c; i <= m; i++) {
f >> x >> y >> c;
G[x].push_back(std::make_pair(y, c));
G[y].push_back(std::make_pair(x, -c));
}
dijkstra(X);
g << dist[Y] << '\n';
f.close();
g.close();
return 0;
}