Pagini recente » Cod sursa (job #1175693) | Cod sursa (job #1682700) | Cod sursa (job #231769) | Cod sursa (job #2748879) | Cod sursa (job #1336431)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
#define nmax 30005
#define inf (1<<30)
#define weight first
#define id second
using namespace std;
bool seen[nmax];
int n, m, x, y, w, source, target, best[nmax], a[4];
vector <pair <int, int>> v[nmax];
string line;
void explore(int x, int soFar) {
seen[x] = true;
if(x == target) return;
for(auto y:v[x]) {
if(best[y.id] == 0) best[y.id] = inf;
best[y.id] = min(best[y.id], soFar + y.weight);
if(!seen[y.id]) explore(y.id, best[y.id]);
}
}
int main() {
ifstream f("sate.in");
ofstream g("sate.out");
f>>n>>m>>source>>target;
f.get();
for(int i=1; i<=m; i++) {
getline(f, line);
memset(a, 0, 4*sizeof(int));
for(int j=0, k=0; j<line.size(); j++)
if('0' <= line[j] && line[j] <= '9')
a[k] = a[k]*10 + line[j]-48;
else ++k;
x = a[0];
y = a[1];
w = a[2];
v[x].push_back(make_pair(w, y));
v[y].push_back(make_pair(-w, x));
}
explore(source, 0);
/*
for(int i=1; i<=n; i++)
cout<<best[i]<<" ";
cout<<"\n";
*/
g<<best[target]<<"\n";
return 0;
}