社区讨论
蛇皮法spfa全部TLE啦,求优化
P3371【模板】单源最短路径(弱化版)参与者 5已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @lo30pgw4
- 此快照首次捕获于
- 2023/10/23 22:53 2 年前
- 此快照最后确认于
- 2023/10/23 22:53 2 年前
CPP
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = (int)1e6;
int total_area, total_road, start_area;
struct road {
int start;
int end;
int scale;
}roads[N];
bool state[N];
int dist[N];
queue <int> line;
int main() {
cin >> total_area >> total_road >> start_area;
memset(dist,0x3f3f3f3f,sizeof(dist));
dist[start_area] = 0;
for (int i = 1; i <= total_road; i++) {
int a, b, c;
cin >> a >> b >> c;
roads[i].start = a;
roads[i].end = b;
roads[i].scale = c;
}
line.push(start_area);
state[start_area] = 1;
while (line.size()) {
int origin = line.front();
line.pop();
state[origin] = 0;
for (int i = 1; i <= total_road; i++) {
if (roads[i].start == origin) {
int a = roads[i].start;
int b = roads[i].end;
int c = roads[i].scale;
if (dist[a] + c < dist[b]) {
dist[b] = dist[a] + c;
}
}
}
for (int i = 1; i <= total_road; i++) {
if (roads[i].start == origin && state[roads[i].end] == 0) {
line.push(roads[i].end);
state[roads[i].end] = 1;
}
}
}
for (int i = 1; i <= total_area; i++) {
cout << dist[i] << " ";
}
return 0;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...