Pagini recente » Cod sursa (job #1747584) | Cod sursa (job #1682556) | Cod sursa (job #508043) | Cod sursa (job #2003845) | Cod sursa (job #863996)
Cod sursa(job #863996)
#include <fstream>
#include <vector>
#include <queue>
#include <list>
using namespace std;
struct nod{
int nodv,val;
}temp;
ifstream f("sate.in");
ofstream g("sate.out");
vector <list < nod > > graf;
vector < int > Cost;
queue < int > q;
int n,m,x,y,xs,ys,c;
void citire(){
f >> n >> m >> x >> y;
graf.resize(n+1);
Cost.resize(n+1);
for(int i=1;i<=m;i++){
f >> xs >> ys >> c;
temp.nodv = ys; temp.val = c;
graf[xs].push_back(temp);
temp.nodv = xs; temp.val = -c;
graf[ys].push_back(temp);
}
}
void Dijkstra(){
int first = x;
q.push(x);
while(!q.empty()){
for(list<nod>::iterator it = graf[first].begin();it!=graf[first].end();++it){
if(Cost[it->nodv] == 0){
Cost[it->nodv] = Cost[first] + it->val;
q.push(it->nodv);
}
if(it->nodv == y){
g << Cost[y];
break;
}}
q.pop();
first = q.front();
}
}
int main(){
citire();
Dijkstra();
f.close();
g.close();
return 0;
}