Pagini recente » Cod sursa (job #1501611) | Cod sursa (job #1299063) | Cod sursa (job #747973) | Cod sursa (job #2273520) | Cod sursa (job #1311228)
#include <fstream>
#include <vector>
#include <cstdlib>
typedef std::pair<int,int> PII;
int main(){
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
int n,m,x,y;
fin>>n>>m>>x>>y;
std::vector< std::vector<PII> > Adj(n+1);
for(int i=0;i<m;++i){
int a,b,c; fin>>a>>b>>c;
Adj[a].push_back(PII(b,c));
Adj[b].push_back(PII(a,c));
}
std::vector<int> dist(n+1,-1);
std::vector<int> q(n);
q[0]=x; dist[x]=0;
unsigned left=0,right=1;
if(x==y){ fout<<"0\n"; return 0; } //ar fi prostie totusi :)
bool found=false;
while(!found){
int t=q[left++];
for(auto it=Adj[t].begin();it!=Adj[t].end();++it)
if(dist[it->first]==-1){
int semn=1;
if(it->first<t) semn*=-1;
if(x>t) semn*=-1;
dist[it->first]=std::abs(dist[t]+semn*it->second);
if(it->first==y) found=true;
else q[right++]=it->first;
}
}
fout<<dist[y]<<'\n';//*/
}