intdijkstra(Node* graph, int N, int s, int t){ bitset<1001> set; set.reset(); priority_queue<Node, vector<Node>, NodeCmp> heap; graph[s].d = 0; set[s] = 1; for (int i = 0; i < graph[s].neighbors.size(); i++) { graph[graph[s].neighbors[i]].d = graph[s].edges[i]; heap.push(graph[graph[s].neighbors[i]]); }
int i = 0; while (i < N - 1) { Node nearestNode = heap.top(); heap.pop(); if (set[nearestNode.id]) continue; i++; set[nearestNode.id] = 1; for (int j = 0; j < nearestNode.neighbors.size(); j++) { if (graph[nearestNode.neighbors[j]].d == -1 || nearestNode.edges[j] + nearestNode.d < graph[nearestNode.neighbors[j]].d) graph[nearestNode.neighbors[j]].d = nearestNode.edges[j] + nearestNode.d; heap.push(graph[nearestNode.neighbors[j]]); } }
return graph[t].d; }
intmain(){ int N = 0, M = 0, S = 0, T = 0; cin >> N >> M >> S >> T; Node* graph = new Node[N + 1]; for (int i = 0; i < N + 1; i++) graph[i].id = i; for (int i = 0; i < M; i++) { int a = 0, b = 0, edge = 0; cin >> a >> b >> edge; graph[a].neighbors.push_back(b); graph[a].edges.push_back(edge); graph[b].neighbors.push_back(a); graph[b].edges.push_back(edge); }