Pagini recente » Cod sursa (job #3030552) | Cod sursa (job #1898899) | Cod sursa (job #127652) | Cod sursa (job #1561593) | Cod sursa (job #1054207)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int NMAX = 30001;
vector<pair<int,int> >V[NMAX];
int Dist[NMAX];
bool Queued[NMAX];
int findMinimalPath(int source, int destination){
int x, y, c;
for(x = 1; x < NMAX; x++)
Dist[x] = NMAX;
queue<int> Q;
Dist[source] = 1;
Q.push(source);
while(!Q.empty()){
x = Q.front();
Q.pop();
Queued[x] = false;
for(int i = 0; i < V[x].size(); i++){
y = V[x][i].first;
c = V[x][i].second;
if(x > y)
c = -c;
if(Dist[y] != NMAX)
continue;
if(Dist[y] > Dist[x] + c || Dist[y] == 0){
Dist[y] = Dist[x] + c;
//if(!Queued[y])
Q.push(y),Queued[y] = true;
}
}
}
return Dist[destination] -1;
}
int main()
{
int x, y, c, X, Y, M, N;
in >> N >> M >> X >> Y;
while(M--){
in >> x >> y >> c;
V[x].push_back(make_pair(y,c));
V[y].push_back(make_pair(x,c));
}
out << findMinimalPath(X,Y);
return 0;
}