Pagini recente » Cod sursa (job #2906046) | Cod sursa (job #108053) | Cod sursa (job #2815053) | Cod sursa (job #1798945) | Cod sursa (job #862868)
Cod sursa(job #862868)
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <queue>
using namespace std;
#define inf 21000000
ifstream f ("sate.in");
ofstream g ("sate.out");
struct nod_g{
int nod, cost;
};
vector < list<nod_g> > graf;
vector <unsigned int> cost;
list <nod_g>::iterator it1;
int n, m, x, y;
nod_g temp;
void citire(){
int x1, y1, c;
f >> n >> m >> x >> y;
graf.resize(n+1);
cost.resize(n+1, inf);
for(int i = 1; i <= m; ++i){
f >> x1 >> y1 >> c;
temp.nod = y1; temp.cost = c;
graf[x1].push_back(temp);
temp.nod = x1; temp.cost = -c;
graf[y1].push_back(temp);
}
f.close();
}
void dijkstra(){
int first = x;
queue <unsigned int> coada;
coada.push(x);
cost[x] = 0;
while(!coada.empty()){
for(it1 = graf[first].begin(); it1 != graf[first].end(); ++it1)
if(cost[it1->nod] > cost[first] + it1->cost){
cost[it1->nod] = cost[first] + it1->cost;
coada.push(it1->nod);
}
coada.pop();
first = coada.front();
}
}
int main(){
citire();
dijkstra();
g << cost[y];
g.close();
return 0;
}