Pagini recente » Cod sursa (job #3147530) | Cod sursa (job #10551) | Cod sursa (job #1438389) | Cod sursa (job #375653) | Cod sursa (job #1378022)
#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;
// <parsing>
const int MAXB = 8192;
FILE* f = fopen("sate.in", "r");
char buf[MAXB]; int ptr = MAXB;
int getInt()
{
while (buf[ptr] < '0' || '9' < buf[ptr]) {
if (++ptr >= MAXB) {
fread(buf, MAXB, 1, f);
ptr = 0;
}
}
int nr = 0;
while ('0' <= buf[ptr] && buf[ptr] <= '9') {
nr = nr * 10 + buf[ptr] - '0';
if (++ptr >= MAXB) {
fread(buf, MAXB, 1, f);
ptr = 0;
}
}
return nr;
}
// </parsing>
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));
n = getInt(); m = getInt(); X = getInt(); Y = getInt();
for (int i = 1, x, y, c; i <= m; i++) {
x = getInt(); y = getInt(); c = getInt();
G[x].push_back(std::make_pair(y, c));
G[y].push_back(std::make_pair(x, -c));
}
dijkstra(X);
g << dist[Y] << '\n';
fclose(f);
g.close();
return 0;
}