main
黄海 2 years ago
parent 22997b64f5
commit 917c198eac

@ -87,32 +87,32 @@ int h[N], e[M], w[M], ne[M], idx;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int d[N]; // 最短距离数组
int dis[N]; // 最短距离数组
bool st[N]; // 是否进过队列
// 迪杰斯特拉
void dijkstra() {
memset(d, 0x3f, sizeof d); // 初始化大
memset(st, 0, sizeof st); // 初始化为未出队列过
priority_queue<PII, vector<PII>, greater<PII>> pq; // 小顶堆
pq.push({0, 0}); // 出发点入队列
d[0] = 0; // 出发点距离0
while (pq.size()) {
auto t = pq.top();
pq.pop();
memset(dis, 0x3f, sizeof dis); // 初始化大
memset(st, 0, sizeof st); // 初始化为未出队列过
priority_queue<PII, vector<PII>, greater<PII>> q; // 小顶堆
q.push({0, 0}); // 出发点入队列
dis[0] = 0; // 出发点距离0
while (q.size()) {
auto t = q.top();
q.pop();
int u = t.second;
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] > d[u] + w[i]) {
d[j] = d[u] + w[i];
pq.push({d[j], j});
if (dis[j] > dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
q.push({dis[j], j});
}
}
}
// 注意此处的S是终点不是起点不是起点不是起点
printf("%d\n", d[S] == INF ? -1 : d[S]);
printf("%d\n", dis[S] == INF ? -1 : dis[S]);
}
int main() {
while (cin >> n >> m >> S) {
@ -126,11 +126,11 @@ int main() {
add(a, b, c);
}
int T;
scanf("%d", &T);
cin >> T;
while (T--) {
int x;
cin >> x;
add(0, x, 0);
add(0, x, 0); // 超级源点法
}
dijkstra();
}
@ -153,25 +153,25 @@ void add(int a, int b, int c) {
}
int n, m; // n个点m条边
int S; // 出发点
int d[N]; // 距离数组
int dis[N]; // 距离数组
bool st[N]; // Dijkstra是不是入过队列
void dijkstra() {
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, S});
d[S] = 0;
dis[S] = 0;
while (q.size()) {
auto t = q.top();
int u = t.second, dist = t.first;
int u = t.second;
q.pop();
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] > dist + w[i]) {
d[j] = dist + w[i];
q.push({d[j], j});
if (dis[j] > dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
q.push({dis[j], j});
}
}
}
@ -181,7 +181,7 @@ int main() {
// 初始化
memset(st, 0, sizeof st);
memset(h, -1, sizeof h);
memset(d, 0x3f, sizeof d);
memset(dis, 0x3f, sizeof dis);
idx = 0;
int ans = INF;
@ -197,7 +197,7 @@ int main() {
cin >> T;
while (T--) {
cin >> x;
ans = min(ans, d[x]);
ans = min(ans, dis[x]);
}
printf("%d\n", ans == INF ? -1 : ans);
}

@ -11,32 +11,32 @@ int h[N], e[M], w[M], ne[M], idx;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int d[N]; // 最短距离数组
int dis[N]; // 最短距离数组
bool st[N]; // 是否进过队列
// 迪杰斯特拉
void dijkstra() {
memset(d, 0x3f, sizeof d); // 初始化大
memset(st, 0, sizeof st); // 初始化为未出队列过
priority_queue<PII, vector<PII>, greater<PII>> pq; // 小顶堆
pq.push({0, 0}); // 出发点入队列
d[0] = 0; // 出发点距离0
memset(dis, 0x3f, sizeof dis); // 初始化大
memset(st, 0, sizeof st); // 初始化为未出队列过
priority_queue<PII, vector<PII>, greater<PII>> q; // 小顶堆
q.push({0, 0}); // 出发点入队列
dis[0] = 0; // 出发点距离0
while (pq.size()) {
auto t = pq.top();
pq.pop();
while (q.size()) {
auto t = q.top();
q.pop();
int u = t.second;
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] > d[u] + w[i]) {
d[j] = d[u] + w[i];
pq.push({d[j], j});
if (dis[j] > dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
q.push({dis[j], j});
}
}
}
// 注意此处的S是终点不是起点不是起点不是起点
printf("%d\n", d[S] == INF ? -1 : d[S]);
printf("%d\n", dis[S] == INF ? -1 : dis[S]);
}
int main() {
while (cin >> n >> m >> S) {
@ -50,11 +50,11 @@ int main() {
add(a, b, c);
}
int T;
scanf("%d", &T);
cin >> T;
while (T--) {
int x;
cin >> x;
add(0, x, 0);
add(0, x, 0); // 超级源点法
}
dijkstra();
}

@ -10,25 +10,25 @@ void add(int a, int b, int c) {
}
int n, m; // n个点m条边
int S; // 出发点
int d[N]; // 距离数组
int dis[N]; // 距离数组
bool st[N]; // Dijkstra是不是入过队列
void dijkstra() {
priority_queue<PII, vector<PII>, greater<PII>> pq;
pq.push({0, S});
d[S] = 0;
while (pq.size()) {
auto t = pq.top();
int u = t.second, dist = t.first;
pq.pop();
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, S});
dis[S] = 0;
while (q.size()) {
auto t = q.top();
int u = t.second;
q.pop();
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] > dist + w[i]) {
d[j] = dist + w[i];
pq.push({d[j], j});
if (dis[j] > dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
q.push({dis[j], j});
}
}
}
@ -38,7 +38,7 @@ int main() {
// 初始化
memset(st, 0, sizeof st);
memset(h, -1, sizeof h);
memset(d, 0x3f, sizeof d);
memset(dis, 0x3f, sizeof dis);
idx = 0;
int ans = INF;
@ -54,7 +54,7 @@ int main() {
cin >> T;
while (T--) {
cin >> x;
ans = min(ans, d[x]);
ans = min(ans, dis[x]);
}
printf("%d\n", ans == INF ? -1 : ans);
}
Loading…
Cancel
Save