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

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