24ICPC成都站赛后补题指南
补题是ICPC竞赛后提升编程能力的重要环节。通过分析成都站赛题,可以掌握更多算法技巧和解题思路。
题目A:最短路径变形
该题需要在带权有向图中找到满足特定条件的最短路径。传统Dijkstra算法需要结合状态压缩进行优化。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
struct Node {
int u, mask, dist;
bool operator<(const Node& rhs) const {
return dist > rhs.dist;
}
};
int dis[N][1<<5];
vector<pair<int,int>> G[N];
int shortestPath(int n, int start, int end) {
memset(dis, 0x3f, sizeof(dis));
priority_queue<Node> pq;
pq.push({start, 0, 0});
dis[start][0] = 0;
while(!pq.empty()) {
Node cur = pq.top(); pq.pop();
if(cur.u == end && cur.mask == (1<<k)-1)
return cur.dist;
for(auto &[v, w] : G[cur.u]) {
int new_mask = cur.mask | getMask(v);
if(dis[v][new_mask] > cur.dist + w) {