计蒜客 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;
}
上一篇:.NET中大型项目开发必备(7)--ORM数据库访问技术


下一篇:人工智能导论实验一:搜索算法求解问题