Pagini recente » Cod sursa (job #2181310) | Cod sursa (job #895086) | Cod sursa (job #421064) | Cod sursa (job #2324330) | Cod sursa (job #2192672)
#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];
}