为什么不能ac 啊
查看原帖
为什么不能ac 啊
984368
wu_zhong_yu楼主2023/4/3 20:35
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int N = 10010, M = 1010;

int n, m;
int e[N], ne[N], h[M], idx, w[N], t[M];
int dist[N];
bool st[N];

typedef pair<int, int> PII;

void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

void dijkstra()
{
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});

    while(heap.size())
    {
        auto q = heap.top();
        heap.pop();
        int ver = q.second, distance = q.first;
        if(st[ver]) continue;
        st[ver] = true;
        for(int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > distance + w[i] + t[j])
            {
                dist[j] = distance + w[i] + t[j];
                heap.push({dist[j], j});
            }
        }
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    memset(h, -1, sizeof h);
    for(int i = 1; i <= n; i ++) scanf("%d", &t[i]);
    for(int i = 0; i < m; i ++)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z);
        add(y, x, z);
    }
    memset(dist, 0x3f, sizeof dist);
    dijkstra();
    cout << dist[n] - t[n] << endl;
    return 0;
}   
2023/4/3 20:35
加载中...