自己写了一遍,按题解写了一遍,16pt,我的 vector 存图有什么问题 qwq ?
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
unordered_map<ll, unordered_map<ll, ll>> dis;
vector<ll> G[N];
ll n, m, s, u, v, k, ans[N];
bool vis[N];
struct node
{
ll to, v;
friend bool operator < (node a, node b)
{
return a.v < b.v;
}
};
priority_queue<node> q;
void dijkstra()
{
memset(ans, 0x7f, sizeof ans);
ans[s] = 0;
q.push({s, 0});
u = k = 0;
while (!q.empty())
{
u = q.top().to;
k = q.top().v;
q.pop();
if (vis[u])
continue;
vis[u] = true;
for (auto g : G[u])
{
if (ans[g] > ans[u] + dis[u][g])
{
ans[g] = ans[u] + dis[u][g];
if (!vis[g])
q.push({g, ans[g]});
}
}
}
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> k;
G[u].push_back(v);
dis[u][v] = k;
}
dijkstra();
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
return 0;
}
map 套 map 是用来存边权的。