题目:
题解:
用单调队列优化掉朴素dijkstra算法的查找最接近最短路节点的未标记节点。
代码:
#include<bits/stdc++.h>
using namespace std;
using PII=pair<int,int>;
const int N=1000010;
int h[N],e[N],ne[N],w[N],idx;
bool st[N];
int dist[N];
int n,m;
void add(int a,int b,int c){
e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
int dijkstra(){
priority_queue<PII,vector<PII>,greater<PII> >heap;
dist[1]=0;
heap.push({0,1});
while(heap.size()){
auto t=heap.top();
heap.pop();
if(st[t.second])continue;
st[t.second]=true;
for(int i=h[t.second];i!=-1;i=ne[i]){
int j=e[i];
if(dist[t.second]+w[i]<dist[j]){
dist[j