其他分享
首页 > 其他分享> > 计蒜客 T1658 热浪

计蒜客 T1658 热浪

作者:互联网

题目链接:计蒜客 T1658 热浪

题目大意:

题解:
单源最短路模板。

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

#define INF 0x3f3f3f3f
#define io_speed_up ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

int cnt, head[2510], dis[2510], n, m, s, t;
bool vis[2510];
struct Edge {
    int v, w, next;
} edge[12500];

void addEdge(int u, int v, int w) {
    edge[++cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt;
}

void spfa(int s) {
    queue<int> q;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) {
        dis[i] = INF;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while (!q.empty()) {
        int u = q.front();
        vis[u] = false;
        q.pop();
        for (int i = head[u]; i; i = edge[i].next) {
            int v = edge[i].v;
            if (dis[v] > dis[u] + edge[i].w) {
                dis[v] = dis[u] + edge[i].w;
                if (!vis[v]) {
                    q.push(v);
                    vis[v] = true;
                }
            }
        }
    }
}

int main() {
    io_speed_up;
    cin >> n >> m >> s >> t;
    for (int i = 1, u, v, w; i <= m; ++i) {
        cin >> u >> v >> w;
        addEdge(u, v, w);
        addEdge(v, u, w);
    }
    spfa(s);
    cout << dis[t];
    return 0;
}

标签:cnt,int,热浪,vis,edge,T1658,计蒜客,addEdge,dis
来源: https://www.cnblogs.com/IzumiSagiri/p/15857496.html