写着道题目气死我自己,一直没有注意是无向图题目,WA了半天....... 来个纯数组的Dijsktra,
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N = 4e5 + 10;
typedef pair<int, int> PII;
int n, m;
int dist[N], w[N];
int h[N],e[N],ne[N],idx;
priority_queue<PII, vector<PII>, greater<PII>> q;
bool st[N];
void add(int a,int b,int c) {
e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;
}
int dijsktra() {
memset(dist, 0x3f, sizeof dist);
q.push({0, 1});
dist[1] = 0;
while (q.size()) {
auto t = q.top();
q.pop();
int distance = t.first, k = t.second;
if (st[k]) continue;
st[k] = true;
for (int i = h[k]; ~i; i = ne[i]) {
int j = e[i];
if (!st[j] && dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
q.push({dist[j], j});
}
}
}
if (dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n >> m;
memset(h, -1, sizeof h);
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
int t = dijsktra();
if (t == -1) cout << "qwb baka";
else cout << t;
return 0;
}