Cod sursa(job #1523695)

Utilizator sabauandrei98Sabau Andrei sabauandrei98 Data 13 noiembrie 2015 00:04:24
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

#define inf (1<<30)
#define mm 30001

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

int n,m;
queue<int> q;
vector< pair<int,int> > v[mm];
int dist[mm];

void BFS(int sursa)
{
    for(int i = 1; i<=n; i++)
        dist[i] = inf;

    q.push(sursa);
    dist[sursa] = 0;

    while(!q.empty())
    {
        int nod = q.front();
        q.pop();

        for(int i = 0; i < v[nod].size(); i++)
            if (dist[nod] + v[nod][i].second < dist[v[nod][i].first])
        {
            dist[v[nod][i].first] = dist[nod] + v[nod][i].second;
            q.push(v[nod][i].first);
        }
    }
}

int main()
{
    int fx,fy;
    f >> n >> m >> fx >> fy;

    for(int i = 1; i<=m; i++)
    {
        int x,y,z;
        f >> x >> y >> z;
        v[x].push_back(make_pair(y,z));
        v[y].push_back(make_pair(x,-z));
    }

    BFS(fx);
    g<<dist[fy];

    return 0;
}