Cod sursa(job #916621)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 16 martie 2013 18:41:44
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

const int MAX_N = 30002;

typedef struct edge
{
    int next, cost;
};

int N, M, S1, S2, x, y;
int D[MAX_N], m[MAX_N];
queue < int > Q;
edge A;
vector < edge > v[MAX_N];

int main()
{
    ifstream f("sate.in");
    ofstream g("sate.out");

    f >> N >> M >> S1 >> S2;
    for(int i = 1; i <= M; ++i)
    {
        f >> x >> y >> A.cost;
        A.next = y, v[x].push_back(A);
        A.next = x, v[y].push_back(A);
    }

    m[S1] = 1;
    Q.push(S1);
    while(!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        for(int i = 0; i < v[x].size(); ++i)
        {
            int y = v[x][i].next;
            if(!m[y])
            {
                if(y < x)
                    D[y] = D[x] - v[x][i].cost;
                else D[y] = D[x] + v[x][i].cost;
                m[y] = 1, Q.push(y);
            }
        }
    }
    
    g << D[S2] << '\n';

    f.close();
    g.close();

    return 0;
}