Cod sursa(job #1300562)

Utilizator TibixbAndrei Tiberiu Tibixb Data 24 decembrie 2014 16:21:26
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include<fstream>
#include<vector>
using namespace std;
int modul(int x){
    return x<0?-x:x;
}
int n, m2, x, y, i, a, b, d, p, u, D[100003], m[100003], c[100003];
vector <pair<int, int> > L[100003];
ifstream in("sate.in");
ofstream out("sate.out");
int main(){
    in>>n>>m2>>x>>y;
    for(i=1; i<=m2; i++){
        in>>a>>b>>d;
        L[a].push_back(make_pair(b, d));
        L[b].push_back(make_pair(a, d));
    }
    D[x]=0;
    m[x]=1;
    c[1]=x;
    p=u=1;
    while(p<=u){
        for(i=0; i<L[c[p]].size(); i++){
            if(m[L[c[p]][i].first]==0){
                c[++u]=L[c[p]][i].first;
                m[L[c[p]][i].first]=1;

                if(c[p]>x && L[c[p]][i].first>x && L[c[p]][i].first<c[p] || c[p]<x && L[c[p]][i].first<x && L[c[p]][i].first>c[p] || c[p]>x && L[c[p]][i].first<x || c[p]<x && L[c[p]][i].first>x)
                    D[L[c[p]][i].first]=modul(D[c[p]]-L[c[p]][i].second);
                else
                    D[L[c[p]][i].first]=D[c[p]]+L[c[p]][i].second;
            }
        }
        p++;
    }
    out<<D[y];
return 0;
}