洛谷 1339 最短路

洛谷 1339 最短路 

 

传送门


 

裸的最短路问题,,没什么可说的,当作是复习了个模板吧

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 const int maxn = 6000;
 6 const int maxm = 50000;
 7 int last[maxn], pre[maxm], other[maxm], len[maxm];
 8 int tot = 0;
 9 int dis[maxn], que[maxn], vis[maxn];
10 int n, m, ts, te;
11 int x, y, z;
12 void spfa(int s) {
13     memset(dis, 127, sizeof(dis));
14     memset(que, 0, sizeof(que));
15     dis[s] = 0;
16     que[1] = s;
17     int h = 0, t = 1;
18     while (h < t) {
19         h = (h + 1) % maxn;
20         int cur = que[h];
21         vis[cur] = 0;
22         for (int p = last[cur]; p; p = pre[p]) {
23             int q = other[p];
24             if (dis[q] > dis[cur] + len[p]) {
25                 dis[q] = dis[cur] + len[p];
26                 if (vis[q] == 0) {
27                     vis[q] = 1;
28                     t = (t + 1) % maxn;
29                     que[t] = q;
30                 }
31             }
32         }
33     }
34 }
35 void add(int x, int y, int z) {
36     tot++;
37     pre[tot] = last[x];
38     last[x] = tot;
39     other[tot] = y;
40     len[tot] = z;
41 }
42 
43 int main () {
44     scanf("%d %d %d %d", &n, &m, &ts, &te);
45     for (int i = 1; i <= m; i++) {
46         scanf("%d %d %d", &x, &y, &z);
47         add(x, y, z);
48         add(y, x, z);
49     }
50     spfa(ts);
51     printf("%d", dis[te]);
52     
53     
54     return 0;
55 }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值