算法课复习 -- 优先队列、最短路

本文解析了多个经典算法问题,包括Huffman编码优化的木头锯割问题,Bellman-Ford算法解决的负权边和负环检测问题,以及优先队列应用于任务调度的最短时间问题。覆盖了数据结构与算法的深入应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ #3253 : Fence Repair

传送门:http://poj.org/problem?id=3253

题意:给n个锯完后的木头的长度。每次锯a+b长度的木头花费a+b。问原来的一整块大木头锯完最少花费多少。

思路:贪心,Huffman编码。用优先队列,每次把最小的两块拿出来相加再塞回队列,队列里最后一个元素就是答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;

const ll M = 1e9 + 7;
const ll INF = 1e9;
const double _e = 10e-6;
const int maxn = 2e4 + 10;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };

int x, y, z;

int n;

struct cmp
{
	bool operator()(int a, int b)
	{
		return a > b;
	}
};

void solve()
{
	priority_queue<int, vector<int>, cmp> que;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &x);
		que.push(x);
	}
	ll ans = 0;
	while (que.size() > 1) {
		int a = que.top(); que.pop();
		int b = que.top(); que.pop();
		que.push(a + b);
		ans += a + b;
	}
	printf("%lld\n", ans);
}

int main()
{
	while (~scanf("%d", &n)) 
		solve();
	return 0;
}

 

POJ #3259 : Wormholes

传送门:http://poj.org/problem?id=3259

题意:给出n个点,m条正的无向边,w条负的有向边。问有没有负环。

思路:Bellman-Ford判断有没有负环。如果有点在第n次还在更新,那就有负环。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;

const ll M = 1e9 + 7;
const ll INF = 1e9;
const double _e = 10e-6;
const int maxn = 510;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };

int x, y, z;

struct edge
{
	int from, to, cost;
};

int t, n, m, w;
edge e[6010];

bool loop()
{
	int d[maxn]; cl0(d);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m * 2 + w; j++) {
			edge v = e[j];
			if (d[v.to] > d[v.from] + v.cost) {
				d[v.to] = d[v.from] + v.cost;
				if (i == n)
					return true;
			}
		}
	}
	return false;
}

int main()
{
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d", &n, &m, &w);
		for (int i = 1; i <= m; i++) {
			scanf("%d%d%d", &x, &y, &z);
			e[i * 2 - 1] = edge{ x,y,z };
			e[i * 2] = edge{ y,x,z };
		}
		for (int i = m * 2 + 1; i <= m * 2 + w; i++) {
			scanf("%d%d%d", &x, &y, &z);
			e[i] = edge{ x,y,-z };
		}
		if (loop())
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}

 

LightOJ #1074 : Extended Traffic

传送门:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1074

题意:给出n个路口m条有向边,每个路口有一个忙碌值。有q个询问,问从点1到询问的点最小的花费和,若小于3或无法到达则输出‘?’。花费为(终点忙碌值-起点忙碌值)^3。

思路:因为是3次方,所以是可能存在负环的。还是用Bellman-Ford求单源最短路+判哪些点在负环里。

多开一个cycle数组,初始化为0,和算最短路一样不断更新,最终所有cycle[i]<0的点i都在负环里。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;

const ll M = 1e9 + 7;
const ll INF = 1e9 + 10;
const double _e = 10e-6;
const int maxn = 210;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };

int x, y, z;
char c;

struct edge
{
	int from, to, cost;
};

int t, n, m, q;
int cost[maxn], d[maxn], cycle[maxn];
edge e[maxn*maxn];

int cal(int a, int b)
{
	return (b - a)*(b - a)*(b - a);
}

void solve()
{
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			edge v = e[j];
			if (d[v.to] > d[v.from] + v.cost) {
				d[v.to] = d[v.from] + v.cost;
				cycle[v.to] = cycle[v.from] + v.cost;
			}
		}
	}
}

int main()
{
	scanf("%d", &t);
	for (int _ = 1; _ <= t; _++) {	
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			d[i] = INF;
		d[1] = 0; cl0(cycle);
		for (int i = 1; i <= n; i++)
			scanf("%d", &cost[i]);
		scanf("%d", &m);
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &x, &y);
			e[i] = edge{ x,y,cal(cost[x],cost[y]) };
		}
		solve(); 
		scanf("%d", &q);
		printf("Case %d:\n", _);
		while (q--) {
			scanf("%d", &x);
			if (d[x] < 3 || d[x] == INF || cycle[x] < 0)puts("?");
			else printf("%d\n", d[x]);
		}
	}
	return 0;
}

 

POJ #1949 : Chores

传送门:http://poj.org/problem?id=1949

题意:有n件家务活,每个都有完成需要的时间和前置家务。问全部完成最快要多久。

思路:优先队列,记录点和结束该任务的时间,按时间从小到大排。记每个点的入度,每当入度为0就进队列,不断维护最大值就是答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;

const ll M = 1e9 + 7;
const ll INF = 1e9 + 10;
const double _e = 10e-6;
const int maxn = 10010;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };

int x, y, z;
char c;

int n, m, ans;
int cost[maxn], in[maxn];
vector<int> son[maxn];

struct node {
	int id;
	int ans;
};

struct cmp {
	bool operator()(node a, node b) {
		return a.ans > b.ans;
	}
};

void solve()
{
	priority_queue<node, vector<node>, cmp> que;
	for (int i = 1; i <= n; i++)
		if (in[i] == 0)que.push(node{ i,cost[i] });
	while (!que.empty()) {
		node u = que.top(); que.pop();
		ans = max(ans, u.ans);
		for (int i = 0; i < son[u.id].size(); i++) {
			int v = son[u.id][i];
			in[v]--;
			if (in[v] == 0)
				que.push(node{ v, u.ans + cost[v] });
		}
	}
}

int main()
{
	while (~scanf("%d", &n)) {
		for (int i = 1; i <= n; i++)
			son[i].clear();
		cl0(in); ans = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d%d", &cost[i], &m);
			while (m--) {
				scanf("%d", &x);
				son[x].push_back(i);
				in[i]++;
			}		
		}
		solve();
		printf("%d\n", ans);
	}
	return 0;
}

 

POJ #1860 : Currency Exchange

传送门:http://poj.org/problem?id=1860

题意:有n种货币以及m种交易,给定初始时有的货币种类(1种)和货币金额。每种交易都指定2种货币,以及交易的汇率和手续费。问有没有可能钱越换越多。

思路:实际上就是判负环。Bellman-Ford判断即可。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;

const ll M = 1e9 + 7;
const ll INF = 1e9;
const double _e = 10e-6;
const int maxn = 1e4 + 10;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };

int x, y, z;
char c;

struct edge
{
	int from, to;
	double r, c;
};

int n, m, s;
double ss, a1, a2, a3, a4;
edge e[210];

bool loop()
{
	double d[maxn]; cl0(d); d[s] = ss;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m * 2; j++) {
			edge v = e[j];
			if (d[v.to] < (d[v.from] - v.c)*v.r) {
				d[v.to] = (d[v.from] - v.c)*v.r;
				if (i == n)
					return true;
			}
		}
	}
	return false;
}

int main()
{
	while (~scanf("%d%d%d%lf", &n, &m, &s, &ss)) {
		for (int i = 1; i <= m; i++) {
			scanf("%d%d%lf%lf%lf%lf", &x, &y, &a1, &a2, &a3, &a4);
			e[i * 2 - 1] = edge{ x,y,a1,a2 };
			e[i * 2] = edge{ y,x,a3,a4 };
		}
		if (loop()) puts("YES");
		else puts("NO");
	}	
	return 0;
}

 

排序算法 快速排序 ⭐⭐⭐⭐ 归并排序 ⭐⭐⭐ 桶排序 ⭐⭐(特殊场景) 注:冒泡/选择/插入排序极少直接考察,但需理解原理 搜索算法 DFS/BFS ⭐⭐⭐⭐⭐(90%比赛必考) 记忆化搜索 ⭐⭐⭐⭐(DP优化常用) 剪枝技巧 ⭐⭐⭐(DFS优化) 动态规划 一维普通DP(爬楼梯/打家劫舍类) ⭐⭐⭐⭐ 背包DP(01背包/完全背包) ⭐⭐⭐ 树形DP(近公共祖先相关) ⭐⭐ 数据结构 栈(表达式计算/括号匹配) ⭐⭐⭐ 列(BFS标准实现) ⭐⭐⭐ 并查集 ⭐⭐⭐⭐(连通性问题) 堆(优先队列实现贪心) ⭐⭐⭐ 树状数组 ⭐⭐(区间求和问题) 图论 小生成树(Prim/Kruskal) ⭐⭐⭐ 单源短路(Dijkstra) ⭐⭐⭐拓扑排序 ⭐⭐ 数学与数论 初等数论(GCD/质数判断/快速幂) ⭐⭐⭐⭐ 排列组合 ⭐⭐⭐ 模运算与逆元 ⭐⭐ 其他重点 二分查找(边界处理) ⭐⭐⭐⭐ 贪心算法(区间调度/ Huffman树) ⭐⭐⭐ 双指针技巧 ⭐⭐⭐ 这是优快云给出的高频算法 1. 搜索算法(DFS/BFS)** [⭐️⭐️⭐️⭐️⭐️] - **出现场景**:几乎每年必考,如迷宫路径、连通性问题、排列组合枚举等。 - **真题示例**: - 第七届“剪邮票”问题(DFS遍历连通性); - 第十二届“砝码称重”隐含记忆化搜索思想; - 第十四届“接龙数列”(字符串搜索与剪枝)。 --- ### **2. 动态规划(DP)** [⭐️⭐️⭐️⭐️] - **高频子类**: - **背包DP**:如第十二届“砝码称重”(01背包变种); - **线性DP**:第七届“煤球数目”(递推问题)、第十四届“接龙数列”(状态转移); - **树形DP**:偶有涉及(如路径计数问题)。 --- ### **3. 贪心算法** [⭐️⭐️⭐️⭐️] - **高频题型**:区间调度、策略选择。 - **真题示例**: - 第四届“翻硬币”(相邻翻转策略); - 第九届“乘积大”(双指针结合正分析)。 --- ### **4. 数学与数论** [⭐️⭐️⭐️⭐️] - **高频内容**: - **初等数论**:因数分解、模运算(第十二届“货物摆放”); - **排列组合**:第七届“凑算式”全排列问题; - **容斥原理**:整数分解问题(第十二届第二场D题)。 --- ### **5. 排序与二分查找** [⭐️⭐️⭐️] - **高频应用**: - **快速排序**:第七届填空题直接考察代码补全; - **二分答案**:第十二届“直线”问题(排序去重优化)。 --- ### **6. 数据结构** [⭐️⭐️⭐️] - **高频结构**: - **栈与列**:模拟题中常见(如第四届“翻硬币”隐含栈思想); - **并查集**:图论连通性问题(如小生成树); - **树状数组/线段树**:区间查询问题(近年偶有涉及)。 --- ### **7. 图论** [⭐️⭐️⭐️] - **高频算法**: - **短路径(Dijkstra/Floyd)**:第十二届“路径”直接考察; - **小生成树(Kruskal/Prim)**:第十二届第二场“城邦”问题; - **拓扑排序**:第十四届“飞机降落”依赖关系问题。 二届“货物摆放”); - **排列组合**:第七届“凑算式”全排列问题; - **容斥原理**:整数分解问题(第十二届第二场D题)。 --- ### **5. 排序与二分查找** [⭐️⭐️⭐️] - **高频应用**: - **快速排序**:第七届填空题直接考察代码补全; - **二分答案**:第十二届“直线”问题(排序去重优化)。 --- ### **6. 数据结构** [⭐️⭐️⭐️] - **高频结构**: - **栈与列**:模拟题中常见(如第四届“翻硬币”隐含栈思想); - **并查集**:图论连通性问题(如小生成树); - **树状数组/线段树**:区间查询问题(近年偶有涉及)。 --- ### **7. 图论** [⭐️⭐️⭐️] - **高频算法**: - **短路径(Dijkstra/Floyd)**:第十二届“路径”直接考察; - **小生成树(Kruskal/Prim)**:第十二届第二场“城邦”问题; - **拓扑排序**:第十四届“飞机降落”依赖关系问题。 这是deepseek给我的哪个准确点呢,因为不一样所以请你再回顾一下十六届以前广东省b组的高频算法按出现算法频率,给我输出一下
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值