首页 > 其他分享 >「TJOI2018」智力竞赛(二分+DAG最小可相交路径覆盖)

「TJOI2018」智力竞赛(二分+DAG最小可相交路径覆盖)

时间:2023-05-09 18:32:51浏览次数:69  
标签:pp DAG int tot fi inf TJOI2018 智力竞赛 define

https://loj.ac/problem/2574

这个题目描述扎心了。

简要题意:
用n+1条可以相交的路径去覆盖DAG,使得没被覆盖的点的权值的最小值最大。

首先二分答案,问题转换为有一些点一定要被覆盖,问n+1条路径内有没有解。

这个可以暴力费用流,每个点拆成两个点,\(i->i',r=1\),如果这个点必选,则费用为inf,否则为0。
由于每个点还可以被额外经过无限次,所以再连\(i->i',r=inf,w=0\)。
若有边\((i,j)\),则\(i'->j\)连边,每个点都可以作为终点或者起点,所以\(S->i,i'->T\)连边。
求总流量不超过n+1时的最大费用,看能不能把inf都选上。

事实上这是个经典问题:DAG路径覆盖问题

先Floyd传递闭包,只保留要被覆盖的点,跑dinic即可。

Code:

#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, _b = y; i <= _b; i ++)
#define ff(i, x, y) for(int i = x, _b = y; i <  _b; i ++)
#define fd(i, x, y) for(int i = x, _b = y; i >= _b; i --)
#define ll long long
#define pp printf
#define hh pp("\n")
using namespace std;

const int inf = 1e9;

const int N = 505;

int n, m;
int v[N];

int f[N][N];

const int M = N * N * 3;

int fi[M], nt[M], to[M], r[M], tot = 1;
int S, T;

void link(int x, int y, int z) {
//	pp("%d %d %d\n", x, y, z);
	nt[++ tot] = fi[x], to[tot] = y, r[tot] = z, fi[x] = tot;
	nt[++ tot] = fi[y], to[tot] = x, r[tot] = 0, fi[y] = tot;
}

void cl() {
	fo(i, 1, T) fi[i] = 0;
	tot = 1;
}

int p[N], p0;

int d[M], d0, dis[M], cur[M];

int bfs() {
	fo(i, 1, T) dis[i] = inf;
	d[d0 = 1] = S; dis[S] = 0;
	for(int i = 1; i <= d0; i ++) {
		int x = d[i];
		cur[x] = fi[x];
		for(int j = fi[x]; j; j = nt[j]) if(r[j] && dis[to[j]] == inf) {
			dis[to[j]] = dis[x] + 1;
			d[++ d0] = to[j];
		}
	}
	return dis[T] < inf;
}

int dfs(int x, int flow) {
	if(x == T) return flow;
	int use = 0;
	for(int i = cur[x]; i; cur[x] = i = nt[i]) if(r[i] && dis[x] + 1 == dis[to[i]]) {
		int t = dfs(to[i], min(flow - use, r[i]));
		use += t, r[i] -= t, r[i ^ 1] += t;
		if(flow == use) return use;
	}
	return use;
}

int pd(int mi) {
	S = 2 * m + 1, T = S + 1;
	cl();
	p0 = 0;
	fo(i, 1, m) if(v[i] < mi) {
		p[++ p0] = i;
		link(S, i, 1);
		link(i + m, T, 1);
	}
	fo(i, 1, p0) fo(j, 1, p0) if(i != j && f[p[i]][p[j]] < inf) {
		link(p[i], p[j] + m, 1);
	}
	int sum = 0;
	while(bfs()) sum += dfs(S, 1 << 30);
	sum = p0 - sum;
	return sum <= n;
}

int main() {
	scanf("%d %d", &n, &m);
	n ++;
	fo(i, 1, m) fo(j, 1, m) if(i != j) f[i][j] = 1e9;
	fo(i, 1, m) {
		int k, j;
		scanf("%d %d", &v[i], &k);
		fo(u, 1, k) {
			scanf("%d", &j);
			f[i][j] = 1;
		}
	}
	fo(k, 1, m) fo(i, 1, m) fo(j, 1, m)
		f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
	int as = 0;
	for(int l = 0, r = 1e9 + 1; l <= r; ) {
		int m = l + r >> 1;
		if(pd(m)) as = m, l = m + 1; else r = m - 1;
	}
	if(as > 1e9) pp("AK\n"); else {
		pp("%d\n", as);
	}
}

标签:pp,DAG,int,tot,fi,inf,TJOI2018,智力竞赛,define
From: https://blog.51cto.com/u_16105286/6259842

相关文章

  • ex2016部署DAG高可用
    目录目录1、环境介绍2、网卡准备3、AD配置3.1、为administrators组授权ExchangeTrustedSubsystem3.2、在DNS中创建A记录dag3.3、创建dag计算机对象并授权给dag成员服务器4、通过ecp配置DAG4.1、创建dag可用性组4.2、配置dag网络4.3、创建dag数据库5、通过命令查看dag状态1、环......
  • Codeforces Round #459 (Div. 2) D. MADMAX DAG&&博弈
    Asweallknow,Maxisthebestvideogameplayeramongherfriends.Herfriendsweresojealousofhers,thattheycreatedanactualgamejusttoprovethatshe’snotthebestatgames.Thegameisplayedonadirectedacyclicgraph(aDAG)withnvertic......
  • Exchange Server 2016 :高可用DAG+NLB
    上一篇文章介绍了现在ExchangeServer2016的架构体系,体系中Exchange的高可用就只剩下了DAG,对于NLB已经采用了其余的负载平衡器。但是在实际测试中,我发现使用两台服务器可以同时部署DAG和NLB,这样部署出来虽然在使用中暂没有发现有什么问题,但是在部署的时候会存在问题,所以这样的DA......
  • UVa 103 Stacking Boxes (DP&DAG)
    103-StackingBoxesTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=39BackgroundSomeconceptsinMathematicsandComputerSciencearesimpleinoneortw......
  • POJ - 3616 Milking Time(DAG)
    题目大意:给出N头牛的产奶时间段和产奶量,每接完一头牛的奶后,需要休息R分钟问如何选择牛,才能使接到的产奶量达到最大解题思路:DAG,按产奶的结束时间大小排序#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintN=1010;structInterval{......
  • 【洛谷】P4590 [TJOI2018]游园会(dp套dp)
    原题链接题意对于一个长度为\(n\)的仅由\(N,O,I\)组成且不包含字串\(NOI\)的字符串\(S\),其与一个给定的长度为\(K\)的字符串的最长公共子序列为\(LCS\)。求出......
  • Java实现一个轻量的DAG任务调度demo
    DAG(DirectedAcyclicGraph,有向无环图)是指一个有向图,其中不包含任何环。在任务调度中,DAG被用来表示任务之间的依赖关系。一个任务的执行必须等待其依赖的任务完成之后才能......
  • exchange2016服务器的DAG的部署
    现在大家都开始用exchange2016的邮件系统了。原先的的exchange2010也需要升级了。要么升级,要exchange2010和exchange2016共存。我是在把原来虚拟机的环境卸载后重新安装的......
  • 带标号弱连通DAG计数
    带标号弱连通DAG计数前言:前段时间做到了一个无向图边定向的题,就一直没搞懂其中的容斥,今天终于弄懂了。题意:对弱连通带标号的简单DAG计数,\(n\leq10^5\)。“弱连通”......
  • DAG的深度优先搜索标记
    对于在图G上进行深度优先搜索算法所产生的深度优先森林Gt,我们可以定义四种边的类型:1.树边(TreeEdge):为深度优先森林中Gt的边。如果结点v是因算法对边(u,v)的搜索而首先被发......