Pagini recente » Cod sursa (job #2194628) | Cod sursa (job #306554) | Cod sursa (job #2698979) | Cod sursa (job #3203934) | Cod sursa (job #1054206)
#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];
class cmp
{
public:
inline bool operator()(const int&a,const int&b){return Dist[a]>Dist[b];}
};
int findMinimalPath(int source, int destination){
int x, y, c;
for(x = 1; x < NMAX; x++)
Dist[x] = NMAX;
priority_queue<int,vector<int>,cmp> Q;
Dist[source] = 1;
Q.push(source);
while(!Q.empty()){
x = Q.top();
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;
}