洛谷P4589 [TJOI2018]智力竞赛 【floyd + 二分 + KM】

题目链接

洛谷P4589
题意可能不清,就是给出一个带权有向图,选出\(n + 1\)条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少

题解

如果要问全部覆盖,就是经典的可重点的DAG最小路径覆盖,floyd求出传递闭包后跑二分图最大匹配即可
如果不能全部覆盖,就二分答案,看看能否覆盖掉比二分出来的值小的所有点

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 505,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
int G[maxn][maxn],g[maxn][maxn],val[maxn],b[maxn],tot,n,m;
int lk[maxn],vis[maxn];
bool find(int u){
	REP(i,n) if (g[u][i] && !vis[i]){
		vis[i] = true;
		if (!lk[i] || find(lk[i])){
			lk[i] = u; return true;
		}
	}
	return false;
}
bool check(int v){
	int cnt = 0;
	REP(i,n) if (val[i] < v) cnt++;
	REP(i,n) REP(j,n)
		if (val[i] < v && val[j] < v) g[i][j] = G[i][j];
		else g[i][j] = 0;
	cls(lk);
	REP(i,n) if (val[i] < v){
		cls(vis); if (find(i)) cnt--;
	}
	return cnt <= m + 1;
}
int main(){
	m = read(); n = read(); int tmp;
	REP(i,n){
		b[i] = val[i] = read();
		tmp = read();
		while (tmp--) G[i][read()] = true;
	}
	REP(k,n) REP(i,n) REP(j,n) G[i][j] |= (G[i][k] & G[k][j]);
	sort(b + 1,b + 1 + n); tot = 1;
	for (int i = 2; i <= n; i++) if (b[i] != b[tot]) b[++tot] = b[i];
	for (int i = 1; i <= n; i++) val[i] = lower_bound(b + 1,b + 1 + tot,val[i]) - b;
	REP(i,n) REP(j,n) g[i][j] = G[i][j];
	if (check(tot + 1)){puts("AK"); return 0;}
	int l = 1,r = tot,mid;
	while (l < r){
		mid = l + r + 1 >> 1;
		if (check(mid)) l = mid;
		else r = mid - 1;
	}
	printf("%d\n",b[l]);
	return 0;
}

posted @ 2018-05-17 11:25  Mychael  阅读(288)  评论(0编辑  收藏  举报