「BZOJ3040」 最短路 - 单源最短路
作者:互联网
题意
给定 \(m\) 条有向边,问 \(1\) 到 \(n\) 的最短路
解析
单源最短路径模板
注意细节优化
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int maxn = 1000010;
int n, m, T, cnt, xa, xc, ya, yc, rp, head[maxn], nxt[maxn], d[maxn], inq[maxn];
struct edge { int to, cost, nxt; } e[maxn * 10];
priority_queue<P, vector<P>, greater<P> > q;
void add(int from, int to, int cost) {
e[++cnt] = (edge){to, cost, head[from]}, head[from] = cnt;
}
int dijkstra() {
memset(d, 0x3f, sizeof(d)), q.push({d[1] = 0, 1});
while (!q.empty()) {
P p = q.top(); q.pop();
int v = p.second;
if (inq[v]) continue; inq[v] = 1;
for (int i = head[v]; i; i = e[i].nxt) {
if (d[e[i].to] > d[v] + e[i].cost) {
q.push({d[e[i].to] = d[v] + e[i].cost, e[i].to});
}
}
}
return d[n];
}
int main() {
scanf("%d %d %d %d %d %d %d %d", &n, &m, &T, &xa, &xc, &ya, &yc, &rp);
for (int i = 1, u, v, w; i <= m - T; i++) {
scanf("%d %d %d", &u, &v, &w), add(u, v, w);
}
printf("%d\n", dijkstra());
return 0;
}
标签:nxt,head,int,短路,单源,inq,cost,maxn,BZOJ3040 来源: https://www.cnblogs.com/alessandrochen/p/11565270.html