堆优化的dij【模板】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 int head[1000005],n, m, cnt, ccnt;
 7 long long dist[1000005],ans;
 8 struct pot {
 9     int id;
10     int val;
11     bool operator<(const struct pot&aa)const {
12         return val > aa.val;
13     }
14 };
15 struct edge {
16     int fr;
17     int to;
18     long long  v;
19     int next;
20 }e[1000005];
21 void INIT() {
22     memset(head, -1, sizeof(head));
23     memset(dist, 0x3f3f3f3f, sizeof(dist));
24     dist[1] = 0;
25     cnt=0;
26     ans = 0;
27 }
28 void adde(int xx, int yy,long long zz) {
29     e[cnt].fr = xx;
30     e[cnt].to = yy;
31     e[cnt].v = zz;
32     e[cnt].next = head[xx];
33     head[xx] = cnt++;
34 }
35 void dij() {
36     priority_queue<struct pot>pq;
37     struct pot sta;
38     sta.id = 1;
39     sta.val = 0;
40     pq.push(sta);
41     while (!pq.empty()) {
42         struct pot aa = pq.top(); pq.pop();
43         if (dist[aa.id] < aa.val)continue;
44         for (int i = head[aa.id]; i != -1; i = e[i].next) {
45             if (dist[e[i].to] > dist[aa.id] + e[i].v) {
46                 dist[e[i].to] = dist[aa.id] + e[i].v;
47                 struct pot cc;
48                 cc.id = e[i].to;
49                 cc.val = dist[e[i].to];
50                 pq.push(cc);
51             }
52         }
53     }
54 }
55 int main() {
56     int t;
57     scanf("%d",&t);
58     while (t--) {
59         INIT();
60         scanf("%d%d", &n, &m);
61         while (m--) {
62             int x, y;
63             long long z;
64             scanf("%d%d%lld", &x, &y, &z);
65             adde(x, y, z);
66         }
67         dij();
68     }
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/8987593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值