structNode { int d = -1; bool inQueue = false; vector<Node*> neighbors; vector<int> edges; };
intspfa(Node* graph, int s, int t){ Node* S = graph + s; S->d = 0; queue<Node*> queue; queue.push(S); while (!queue.empty()) { Node* cur = queue.front(); cur->inQueue = false; queue.pop(); for (int i = 0; i < cur->neighbors.size(); i++) { if (cur->neighbors[i]->d == -1 || cur->neighbors[i]->d > cur->d + cur->edges[i]) { cur->neighbors[i]->d = cur->d + cur->edges[i]; if (!cur->neighbors[i]->inQueue) { cur->neighbors[i]->inQueue = true; queue.push(cur->neighbors[i]); } } } }
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 < M; i++) { int a = 0, b = 0, edge = 0; cin >> a >> b >> edge; graph[a].neighbors.push_back(graph + b); graph[a].edges.push_back(edge); graph[b].neighbors.push_back(graph + a); graph[b].edges.push_back(edge); }