Pagini recente » Cod sursa (job #2031612) | Cod sursa (job #2625034) | Cod sursa (job #826060) | Cod sursa (job #2393585) | Cod sursa (job #2192675)
#include<fstream>
#include<list>
#include<deque>
#include<bitset>
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];
bitset<MaxNodesCnt> vis;
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);
vis[node] = true;
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(!vis[newNode]){
dist[newNode] = dist[node] + cost;
vis[newNode] = true;
Queue.push_back(newNode);
}
}
}
}
int main(){
readData();
BFS(startNode);
fout << dist[endNode];
}