洛谷__P2349 金字塔(dijkstra)
题目链接:P2349 金字塔 - 洛谷
题目大意:
从点1走到点n,其中会随机使通过两点的时间*2,
求:在考虑最坏的情况下,从点1走到点n的最短时间
和 ,从 室跑到 需要 秒
思路:
1.数据不大,直接枚举每次从点1到点n的过程中通过两点间的最大时间,
明显,答案就是对每次 枚举的时间+dis[n] 取最小值;
2.我们要找到是从点 1 到点 n 的 最短路+ 最大的边权 最小,
放到结构体中排序实现堆优化的dijkstra
再用mx[N] 数组存从点 1 到点 i 的过程中 通过某两点的最大时间,
只需跑一遍dijkstra即可;
代码1:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
using namespace std;
typedef pair<int, int> pii;
const int N = 2086, mod = 998244353;
int n, m;
int h[N], ne[N * 2], e[N * 2], w[N * 2], idx;
int dis[N];
int mx[N];
void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
void dij(int x) {
priority_queue<pii, vector<pii>, greater<pii >> q;
vector<bool> st(n + 1);
mst(dis, 1);
dis[1] = 0;
q.push({dis[1], 1});
while (q.size()) {
auto[d, u] = q.top();
q.pop();
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (w[i] > x) continue;//超过了枚举的时间就不走这条路
if (dis[j] > d + w[i]) {
dis[j] = d + w[i];
q.push({dis[j], j});
}
}
}
}
void solve() {
mst(h, -1);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
int res = inf;
for (int i = 1; i <= 300; i++) {
dij(i);
res = min(res, dis[n] + i);
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}
代码2:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
using namespace std;
typedef pair<int, int> pii;
const int N = 2086, mod = 998244353;
int n, m;
int h[N], ne[N * 2], e[N * 2], w[N * 2], idx;
int dis[N], mx[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
struct node {
int d, u, ma;
bool operator<(const node &b)const {
return d + ma > b.d + b.ma;
}
};
priority_queue<node> q;
void dij() {
mst(dis, 1);
dis[1] = 0;
q.push({dis[1], 1, 0});
while (q.size()) {
auto[d, u, ma] = q.top();
q.pop();
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
int tp = max(ma, w[i]);
if (dis[j] + mx[j] > d + tp + w[i]) {
dis[j] = d + w[i];
mx[j] = tp;
q.push({dis[j], j, tp});
}
}
}
}
void solve() {
mst(h, -1);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
dij();
cout << dis[n] + mx[n] << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}

浙公网安备 33010602011771号