题解 ZYB和售货机

传送门

  • 每个点只有一条入边/出边,会形成一棵基环树

首先考虑对选了会产生收益的点之间连边
然后发现每个连通块里有且仅有一个环(自环也算)
考场上想分情况讨论,对每个环上的点维护指向它的最大值和次大值,再维护个 \(min_{dlt}\)
然而思路不怎么清楚,没调出来

正解和暴力很像,但思路要清晰地多
对于这个环,有两种可能
若存在环边不是最优决策,那环上的点一定可以被取完,当做树处理即可
若每个环边都是最优决策,需要在环上找一个剩下的点
显然要找环边收益-连向它的树边收益最小的点(即损失最小的点)
减去这个差值即可

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
#define reg register int
#define fir first
#define sec second
#define make make_pair
//#define int long long 

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n;
int f[N], c[N], d[N], a[N];

namespace force{
	int *****dp, ans;
	inline void tran(int u, int au, int v, int i, int j, int k, int h, int l) {
		if (!au) return ;
		if (v==1) {
			if (!i) return ;
			dp[i-1][j][k][h][l] = max(dp[i-1][j][k][h][l], dp[i][j][k][h][l]+d[v]-c[u]);
		}
		else if (v==2) {
			if (!j) return ;
			dp[i][j-1][k][h][l] = max(dp[i][j-1][k][h][l], dp[i][j][k][h][l]+d[v]-c[u]);
		}
		else if (v==3) {
			if (!k) return ;
			dp[i][j][k-1][h][l] = max(dp[i][j][k-1][h][l], dp[i][j][k][h][l]+d[v]-c[u]);
		}
		else if (v==4) {
			if (!h) return ;
			dp[i][j][k][h-1][l] = max(dp[i][j][k][h-1][l], dp[i][j][k][h][l]+d[v]-c[u]);
		}
		else if (v==5) {
			if (!l) return ;
			dp[i][j][k][h][l-1] = max(dp[i][j][k][h][l-1], dp[i][j][k][h][l]+d[v]-c[u]);
		}
	}
	void solve() {
		dp=new int****[a[1]+1];
		for (int i=0; i<=a[1]; ++i) {
			dp[i]=new int***[a[2]+1];
			for (int j=0; j<=a[2]; ++j) {
				dp[i][j]=new int**[a[3]+1];
				for (int k=0; k<=a[3]; ++k) {
					dp[i][j][k]=new int*[a[4]+1];
					for (int h=0; h<=a[4]; ++h)
						dp[i][j][k][h]=new int[a[5]+1];
				}
			}
		}
		dp[a[1]][a[2]][a[3]][a[4]][a[5]]=0;
		for (int i=a[1]; ~i; --i) {
			for (int j=a[2]; ~j; --j) {
				for (int k=a[3]; ~k; --k) {
					for (int h=a[4]; ~h; --h) {
						for (int l=a[5]; ~l; --l) {
							tran(1, i, f[1], i, j, k, h, l);
							tran(2, j, f[2], i, j, k, h, l);
							tran(3, k, f[3], i, j, k, h, l);
							tran(4, h, f[4], i, j, k, h, l);
							tran(5, l, f[5], i, j, k, h, l);
							ans=max(ans, dp[i][j][k][h][l]);
						}
					}
				}
			}
		}
		printf("%d\n", ans);
		exit(0);
	}
}

namespace task1{
	ll ans;
	void solve() {
		for (int i=1; i<=n; ++i) {
			if (d[i]>c[i]) 
				ans += 1ll*(d[i]-c[i])*a[i];
		}
		printf("%lld\n", ans);
		exit(0);
	}
}

namespace task2{
	ll ans, max_redlt[N];
	int cnt[N], fa[N], siz[N];
	pair<int, int> maxinc[N], secinc[N];
	bool vis[N], vist[N];
	inline int find(int p) {return fa[p]==p?p:fa[p]=find(p);}
	void solve() {
		for (int i=1; i<=n; ++i) fa[i]=i, siz[i]=1;
		for (int i=1; i<=n; ++i) maxinc[i].fir=-INF, secinc[i].fir=-INF;
		for (int i=1; i<=n; ++i) {
			if (d[i]-c[i] >= maxinc[f[i]].fir) {
				secinc[f[i]]=maxinc[f[i]];
				maxinc[f[i]]=make(d[i]-c[i], i);
			}
			else if (d[i]-c[i] >= secinc[f[i]].fir) {
				secinc[f[i]]=make(d[i]-c[i], i);
			}
		}
		for (int i=1; i<=n; ++i) if (maxinc[i].fir>0) {
			vis[i]=1;
			ans+=1ll*maxinc[i].fir*a[i];
		}
		for (int i=1; i<=n; ++i) {
			if (vis[i] && vis[f[i]]) {
				int f1=find(i), f2=find(f[i]);
				fa[f1]=f2; siz[f2]+=siz[f1];
			}
		}
		printf("%lld\n", ans);
		exit(0);
	}
}

namespace task{
	int head[N], size, sta[N], top, fa[N];
	ll sum[N], ans, max_inc[N];
	bool vis[N], ring[N];
	struct egde{int to, next, val; bool back;}e[N<<1];
	inline void add(int s, int t, int w, bool typ) {e[++size].to=t; e[size].next=head[s]; e[size].val=w; e[size].back=typ; head[s]=size;}
	inline int find(int p) {return fa[p]==p?p:fa[p]=find(fa[p]);}
	int dfs(int u) {
		vis[u]=1;
		for (int i=head[u],v; ~i; i=e[i].next) if (!e[i].back) {
			v = e[i].to;
			if (u==v) {sta[++top]=u; ring[u]=1; return -1;}
			if (vis[v]) {sta[++top]=u; ring[u]=1; return v;}
			int tem=dfs(v);
			if (~tem) {
				sta[++top]=u; ring[u]=1;
				return tem==u?-1:tem;
			}
			break;
		}
		return -1;
	}
	void solve() {
		memset(head, -1, sizeof(head));
		for (int i=1; i<=n; ++i) fa[i]=i;
		for (int i=1,f1,f2; i<=n; ++i) 
			if (c[i]<d[f[i]]) {
				add(i, f[i], d[f[i]]-c[i], 0);
				add(f[i], i, d[f[i]]-c[i], 1);
				f1=find(i); f2=find(f[i]);
				if (f1!=f2) {fa[f1]=f2; sum[f2]+=sum[f1];}
				if (1ll*(d[f[i]]-c[i])*a[f[i]] > max_inc[f[i]]) {
					sum[f2]+=1ll*(d[f[i]]-c[i])*a[f[i]]-max_inc[f[i]];
					max_inc[f[i]]=1ll*(d[f[i]]-c[i])*a[f[i]];
				}
			}
		for (int i=1,f1; i<=n; ++i) if (!vis[f1=find(i)]) {
			//cout<<"sum: "<<sum[f1]<<endl;
			dfs(i);
			//cout<<"top: "<<top<<endl;
			if (top<=1) ans+=sum[f1];
			else {
				ll min_dlt=INF, t;
				for (int j=1,u; j<=top; ++j) {
					u=sta[j]; t=d[f[u]]-c[u];
					min_dlt=min(min_dlt, t);
					for (int k=head[f[u]],v; ~k; k=e[k].next) if (e[k].back) {
						v = e[k].to;
						if (!ring[v]) min_dlt=min(min_dlt, t-d[f[u]]+c[v]);
					}
				}
				//cout<<"min_dlt: "<<min_dlt<<endl;
				min_dlt=max(min_dlt, 0ll);
				ans+=sum[f1]-min_dlt;
			}
			while (top) ring[sta[top--]]=0;
			
			vis[f1]=1;
		}
		printf("%lld\n", ans);
		exit(0);
	}
}

signed main()
{
	freopen("goods.in", "r", stdin);
	freopen("goods.out", "w", stdout);
	
	n=read();
	bool all_same=1;
	for (int i=1; i<=n; ++i) {
		f[i]=read(), c[i]=read(), d[i]=read(), a[i]=read();
		if (f[i]!=i) all_same=0;
	}
	//if (all_same) task1::solve();
	//else if (n<=5) force::solve();
	//else task1::solve();
	task::solve();
	
	return 0;
}
posted @ 2021-09-14 21:07  Administrator-09  阅读(6)  评论(0编辑  收藏  举报