shuanhao @ 2024-08-28 12:12:01
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 20010;
long long h[N], e[N], ne[N], w[N], idx;
long long dist[N];
typedef pair<long long, long long>PII;
int n, m, s;
bool st[N];
void add(long long a, long long b, long long c)
{
ne[idx] = h[a];
e[idx] = b;
w[idx] = c;
h[a] = idx++;
}
void dijkstra()
{
memset(dist, 0x3f, sizeof(dist));
priority_queue<PII, vector<PII>, greater<PII>>heap;
heap.push({ 0,s });
dist[s] = 0;
while (heap.size())
{
PII t = heap.top();
heap.pop();
long long ver = t.second;
long long distance = t.first;
if (st[ver])continue;
st[ver] = true;
for (long long i = h[ver]; i != -1; i = ne[i])
{
long long j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
heap.push({ dist[j],j });
}
}
}
}
int main()
{
memset(h, -1, sizeof(h));
cin >> n >> m >> s;
for (long long i = 0; i < m; i++)
{
long long a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
dijkstra();
for (long long i = 1; i <= n; i++)
cout << dist[i] << " ";
return 0;
}
by Melo_DDD @ 2024-08-28 12:15:43
@shuanhao 数组开小了,少加了一个 0,而且链式前向星要建二倍边
by 玉树临风英俊潇洒 @ 2024-08-28 12:28:38
数据范围是到1e9的,链式前向星存图需要二倍
把数组开大点就行(加一个0就过了
求关
by 玉树临风英俊潇洒 @ 2024-08-28 12:30:12
改了就AC了
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 200100;
long long h[N], e[N], ne[N], w[N], idx;
long long dist[N];
typedef pair<long long, long long>PII;
int n, m, s;
bool st[N];
void add(long long a, long long b, long long c)
{
ne[idx] = h[a];
e[idx] = b;
w[idx] = c;
h[a] = idx++;
}
void dijkstra()
{
memset(dist, 0x3f, sizeof(dist));
priority_queue<PII, vector<PII>, greater<PII>>heap;
heap.push({ 0,s });
dist[s] = 0;
while (heap.size())
{
PII t = heap.top();
heap.pop();
long long ver = t.second;
long long distance = t.first;
if (st[ver])continue;
st[ver] = true;
for (long long i = h[ver]; i != -1; i = ne[i])
{
long long j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
heap.push({ dist[j],j });
}
}
}
}
int main()
{
memset(h, -1, sizeof(h));
cin >> n >> m >> s;
for (long long i = 0; i < m; i++)
{
long long a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
dijkstra();
for (long long i = 1; i <= n; i++)
cout << dist[i] << " ";
return 0;
}
by shuanhao @ 2024-08-28 13:50:42
@Melo_DDD 感谢!已过!已关!
by shuanhao @ 2024-08-28 13:51:16
@Dalong37 感谢!已过!已关!