Pagini recente » Cod sursa (job #698693) | Cod sursa (job #867731) | Cod sursa (job #524069) | Cod sursa (job #1842382) | Cod sursa (job #863997)
Cod sursa(job #863997)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
struct nod{
int nodv,val;
}temp;
ifstream f("sate.in");
ofstream g("sate.out");
vector <vector < 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(vector<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];
return;
}}
q.pop();
first = q.front();
}
}
int main(){
citire();
Dijkstra();
f.close();
g.close();
return 0;
}