dijkstra 80分求助
查看原帖
dijkstra 80分求助
595980
yghygh123楼主2023/2/23 23:37
/*
coder:sunshine
school:njupt
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' //交互题删掉
#define x first
#define y second
typedef long long ll;
typedef pair<int, int> pii;
constexpr int mod = 1e9 + 7;

constexpr int N = 1e3 + 10;
vector<int> g[N], w[N];
bool st[N];
int dist[N];
int n, m;
int f[N];

void dij()
{
    for (int i = 1; i <= n; i++)
    {
        dist[i] = 1e9;
    }
    dist[1] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push({0, 1});
    while (q.size())
    {
        auto t = q.top();
        q.pop();
        int to = t.y, dis = t.x;
        if (st[to])
            continue;
        st[to] = 1;
        for (int i = 1; i < g[to].size(); i++)
        {
            int j = g[to][i], chang = w[to][i];
            if (dist[j] > dis + chang + f[j])
            {
                dist[j] = dis + chang + f[j];
                q.push({dist[j], j});
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> f[i];
    }
    f[1] = f[n] = 0;
    for (int i = 1; i <= m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back(b);
        w[a].push_back(c);
        g[b].push_back(a);
        w[b].push_back(c);
    }
    dij();
    cout << dist[n] << endl;
    return 0;
}

不知道为什么wa两个点,help me

2023/2/23 23:37
加载中...