Cod sursa(job #2192672)

Utilizator DawlauAndrei Blahovici Dawlau Data 6 aprilie 2018 20:54:29
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include<fstream>
#include<list>
#include<deque>
using namespace std;

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

const int MaxNodesCnt = 3e4 + 5;

struct Graph{

    int newNode, cost;
};

list<Graph> adjList[MaxNodesCnt];
int dist[MaxNodesCnt];
int nodesCnt, edgesCnt, startNode, endNode;

inline void readData(){

    fin >> nodesCnt >> edgesCnt >> startNode >> endNode;

    int from, to, cost;
    while(edgesCnt--){

        fin >> from >> to >> cost;
        adjList[from].push_back({to, cost});
        adjList[to].push_back({from, -cost});
    }
}

inline void BFS(int node){

    deque<int> Queue;
    Queue.push_back(node);

    int newNode, cost;

    while(!Queue.empty()){

        node = Queue.front();
        Queue.pop_front();

        for(const Graph &nextNode : adjList[node]){

            newNode = nextNode.newNode;
            cost = nextNode.cost;
            if(dist[newNode] == 0 and newNode != startNode){

                dist[newNode] = dist[node] + cost;
                Queue.push_back(newNode);
            }
        }
    }
}

int main(){

    readData();
    BFS(startNode);
    fout << dist[endNode];
}