【P2323 [HNOI2006]公路修建问题】题解


题目链接

题外话:

一道纯最小生成树的题,能出道蓝我也真服了...


本文默认使用kruskal算法,主要是因为另一种我不会

首先我们先满足 \(k\) 条一级道路,对所有道路按一级道路造价排序,然后用最小生成树的做法选出 \(k\) 条边。

对于剩下的道路按二级造价排序,然后同理继续选即可。

时间复杂度 \(O(n\log n)\)

需要注意的是题目输出格式有误,后面是输出具体方案。第一个数代表选的边的编号,第二个数表示一级还是二级道路。

Code:

// Problem: P2323 [HNOI2006]公路修建问题
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2323
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 20010
struct node
{
	int x, y, yi, er, id; 
}d[N]; 
struct Node
{
	int x, y; 
}cnt[N]; 
int n, m, i, j, k; 
int fa[N], ans, p, x, y; 

int dfs(int x)
{
	if(fa[x]==x) return x; 
	return fa[x]=dfs(fa[x]); 
}

bool cmp(node x, node y)
{
	return x.yi