堆优化的dij【模板】

时间:2021-03-07 21:54:52
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int head[],n, m, cnt, ccnt;
long long dist[],ans;
struct pot {
int id;
int val;
bool operator<(const struct pot&aa)const {
return val > aa.val;
}
};
struct edge {
int fr;
int to;
long long v;
int next;
}e[];
void INIT() {
memset(head, -, sizeof(head));
memset(dist, 0x3f3f3f3f, sizeof(dist));
dist[] = ;
cnt=;
ans = ;
}
void adde(int xx, int yy,long long zz) {
e[cnt].fr = xx;
e[cnt].to = yy;
e[cnt].v = zz;
e[cnt].next = head[xx];
head[xx] = cnt++;
}
void dij() {
priority_queue<struct pot>pq;
struct pot sta;
sta.id = ;
sta.val = ;
pq.push(sta);
while (!pq.empty()) {
struct pot aa = pq.top(); pq.pop();
if (dist[aa.id] < aa.val)continue;
for (int i = head[aa.id]; i != -; i = e[i].next) {
if (dist[e[i].to] > dist[aa.id] + e[i].v) {
dist[e[i].to] = dist[aa.id] + e[i].v;
struct pot cc;
cc.id = e[i].to;
cc.val = dist[e[i].to];
pq.push(cc);
}
}
}
}
int main() {
int t;
scanf("%d",&t);
while (t--) {
INIT();
scanf("%d%d", &n, &m);
while (m--) {
int x, y;
long long z;
scanf("%d%d%lld", &x, &y, &z);
adde(x, y, z);
}
dij();
}
return ;
}