Cod sursa(job #483052)

Utilizator andra23Laura Draghici andra23 Data 6 septembrie 2010 19:11:27
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include<fstream>
#include<vector>
#define nmax 100005

using namespace std;
struct elem{
    int nod, d;
    elem(){}
    elem(int a, int b){
        nod = a; 
        d = b;
    }
};

vector<elem> v[nmax];
int c[nmax], dist[nmax];

int main(){
    ifstream f("sate.in");
    ofstream g("sate.out");
    int m, n, a, b, d;
    f>>n>>m>>a>>b;
    int i, j, p, u, x;
    elem y;
    
    for (i = 1; i <= m; i++){
        f>>p>>u>>d;
        v[p].push_back(elem(u, d));
        v[u].push_back(elem(p, d));
    }
    
    for (i = 1; i <= n; i++)
        dist[i] = -1;
    p = u = 1;
    c[1] = a;
    dist[a] = 0;
    
    while (p <= u){
        x = c[p];
        if (x == b)
            break;
        p++;
        for (i = 0; i < v[x].size(); i++){
            y = v[x][i];
            if (dist[y.nod] == -1){
                if (a < x)
                    if (y.nod < a)
                        dist[y.nod] = y.d - dist[x];
                    else 
                        if (y.nod < x)
                            dist[y.nod] = dist[x] - y.d;
                        else 
                            dist[y.nod] = dist[x] + y.d;
                else 
                    if (y.nod < x)
                        dist[y.nod] = dist[x]+y.d; 
                    else 
                        if (y.nod < a)
                            dist[y.nod] = dist[x] - y.d;
                        else 
                            dist[y.nod] = y.d - dist[x];
                u++;
                c[u] = y.nod;
            }   
        }     
    }
    
    g<<dist[b]<<'\n';
    
    return 0;
}