Cod sursa(job #1086007)

Utilizator andreiiiiPopa Andrei andreiiii Data 17 ianuarie 2014 17:20:11
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#define PII pair<int, int>
#define x first
#define y second

using namespace std;

const int N=30005;

ifstream fin("sate.in");
ofstream fout("sate.out");

vector <PII> G[N];
int d[N];


int main()
{
    int n, m, i, x, y, z, X, Y;
    queue <int> Q;
    fin>>n>>m>>X>>Y;
    while(m--)
    {
        fin>>x>>y>>z;
        G[x].push_back(make_pair(y, z));
        G[y].push_back(make_pair(x, z));
    }
    memset(d, -1, sizeof(d));
    d[X]=0;
    for(Q.push(X);!Q.empty()&&d[Y]==-1;Q.pop())
    {
        x=Q.front();
        for(auto p: G[x])
        {
            if(d[p.x]==-1)
            {
                d[p.x]=x<p.x?d[x]+p.y:d[x]-p.y;
                Q.push(p.x);
            }
        }
    }
    fout<<d[Y];
    fin.close();
    fout.close();
}