HDU 6611 K Subsequence (最小费用最大流)

 HDU 6611 K Subsequence (最小费用最大流)_第1张图片

#include 
#include 
#include 
#include  
#include 
#define il inline
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e4;
const int INF = 0x7fffffff;
struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to, int _capacity, int _cost, int _rev) :to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};
struct Min_Cost_Max_Flow {
	int V, H[maxn + 5], dis[maxn + 5], PreV[maxn + 5], PreE[maxn + 5];
	vector G[maxn + 5];
	//调用前初始化
	void Init(int n) {
		V = n;
		for (int i = 0; i <= V; ++i)G[i].clear();
	}
	//加边
	void Add_Edge(int from, int to, int cap, int cost) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
	}
	//flow是自己传进去的变量,就是最后的最大流,返回的是最小费用
	int Min_cost_max_flow(int s, int t, int f, int& flow) {
		int res = 0; fill(H, H + 1 + V, 0);
		while (f) {
			priority_queue , vector >, greater > > q;
			fill(dis, dis + 1 + V, INF);
			dis[s] = 0; q.push(pair(0, s));
			while (!q.empty()) {
				pair now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
						PreV[e.to] = v;
						PreE[e.to] = i;
						q.push(pair(dis[e.to], e.to));
					}
				}
			}
			if (dis[t] == INF)break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
			f -= d; flow += d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
				edge& e = G[PreV[v]][PreE[v]];
				e.capacity -= d;
				G[v][e.rev].capacity += d;
			}
		}
		return res;
	}
	int Max_cost_max_flow(int s, int t, int f, int& flow) {
		int res = 0;
		fill(H, H + 1 + V, 0);
		while (f) {
			priority_queue  > q;
			fill(dis, dis + 1 + V, -INF);
			dis[s] = 0;
			q.push(pair(0, s));
			while (!q.empty()) {
				pair now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] > now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] < dis[v] + e.cost + H[v] - H[e.to]) {
						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
						PreV[e.to] = v;
						PreE[e.to] = i;
						q.push(pair(dis[e.to], e.to));
					}
				}
			}
			if (dis[t] == -INF)break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
			f -= d; flow += d;
			res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
				edge& e = G[PreV[v]][PreE[v]];
				e.capacity -= d;
				G[v][e.rev].capacity += d;
			}
		}
		return res;
	}
};
Min_Cost_Max_Flow MCMF;
int a[2005];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,k;
		scanf("%d %d",&n,&k);
		int s1 = 0,s2 = 2*n+1,t=2*n+2;
		MCMF.Init(t);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			MCMF.Add_Edge(i,i+n,1,-a[i]);
		}
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++)
				if(a[j]>=a[i])MCMF.Add_Edge(i+n,j,1,0); 
		}
		for(int i=1;i<=n;i++)MCMF.Add_Edge(s2,i,1,0),MCMF.Add_Edge(i+n,t,1,0);
		MCMF.Add_Edge(s1,s2,k,0);
		int flow = 0;
		printf("%d\n",-MCMF.Min_cost_max_flow(s1,t,INF,flow));
	}
	return 0;
}

 

你可能感兴趣的:(网络流,2019HDU多校)