Pagini recente » Cod sursa (job #2649962) | Cod sursa (job #2360401) | Cod sursa (job #1743139) | Cod sursa (job #692581) | Cod sursa (job #524957)
Cod sursa(job #524957)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define nmax 30002
using namespace std;
const char iname[] = "sate.in";
const char oname[] = "sate.out";
ifstream fin(iname);
ofstream fout(oname);
int n, m, x, y, a, b, c, i;
vector <pair <int, int> > Graf[nmax];
queue<int> Q;
int viz[nmax];
int D[nmax];
void BF()
{
Q.push(x);
while(!Q.empty())
{
x = Q.front();
Q.pop();
for(int i = 0; i < Graf[x].size(); i ++)
{
if(viz[Graf[x][i].first] == 0)
{
viz[Graf[x][i].first] = 1;
D[Graf[x][i].first] = D[x] + Graf[x][i].second;
Q.push(Graf[x][i].first);
}
}
}
}
int main()
{
fin >> n >> m >> x >> y;
for(i = 1; i <= m; i ++)
{
fin >> a >> b >> c;
if(b < a)
swap(a, b);
Graf[a].push_back(make_pair(b, c));
Graf[b].push_back(make_pair(a, -c));
}
BF();
fout << D[y];
return 0;
}