链式前向星 + 堆优化dijkstra
1、不知道为什么 WA 了?
2、另外,我发现 greater< pair<int, int> > 即使没定义它也是默认排序 .first 吗?
3、我尝试自定义算子,但迷之CE?(调不来,本地没有报错,而是弹出一大堆乱码)
4、循环中的 ~i 是什么意思?
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N = 1e5 + 10, inf = 0x3f3f3f3f;
struct edge
{
int to, w, next;
}e[N];
int n, m, s, top, h[N], dist[N];
bool vis[N];
priority_queue< pii, vector<pii>, greater<pii> > q;
void add(int x, int y, int w)
{
e[++top] = {y, w, h[x]};
h[x] = top;
}
void dijkstra()
{
for (int i = 0; i <= n; i ++) dist[i] = inf;
dist[s] = 0;
q.push({0, s});
while (!q.empty())
{
pii t = q.top();
q.pop();
int x = t.second;
if (vis[x]) continue;
vis[x] = true;
for (int i = h[x]; i ; i = e[i].next)
{
int y = e[i].to, w = e[i].w;
if (dist[x] + w < dist[y])
{
dist[y] = dist[x] + w;
q.push({dist[y], y});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> s;
for (int i = 1; i <= m; i ++)
{
int x, y, w;
cin >> x >> y >> w;
add(x, y, w);
}
dijkstra();
for (int i = 1; i <= n; i ++) cout << dist[i] << ' ';
return 0;
}
struct cmp
{
friend bool operator < (pii a, pii b)
{
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
};
priority_queue< pii, vector<pii>, cmp > q;