列表

详情


NC205330. GridPainting

描述

Recently, Little Gyro picked up several grids whose total number is n on the ground. As Little Gyro is bored, he decided to paint the grids with just two totally different colours, black and white. Here shows the picture of the grids after coloured:

Then, Little Gyro made another definition of the beauty value of those grids. At first, Little Gyro labelled these grids within the subscript from 1 to n, it means number i indicates the i-th grid. And he defined each grid contains two properties a and b, Property a indicates that if Little Gyro coloured the grid into black, it'll produce the beauty value of a_i, and Property b indicates that if Little Gyro coloured the grid into white, it'll produce the beauty value of b_i. After that, Little Gyro added up all the generated beauty value and obtained the total beauty value of all the grids.
What's more, Little Gyro also found that most of the grids had an additional property. The additional property of the i-th ( i ≥ 3 ) grid is described as a triple (p_1,p_2,val), indicating that if the i-th grid is currently coloured black, and at least one of the two subscripts p_1, p_2 is coloured white, the summary of the beauty value will be reduced by val. And the two grids with the subscript 1 or 2 do not have this additional property.
Now given the total number of all the grids, Little Gyro wanted to colour all of them and tried to maximum the beauty value. Since there were too many situations to consider, Little Gyro couldn't solve the problem by himself. Please help him.

输入描述

Each input file only contains one test case.
The first line contains an integer n (3 ≤ n ≤ 1000), indicating the total number of all the grids.
The second line contains n integers a_1,a_2,...,a_n (1 ≤ a_i), indicating the characteristic a of each grid.
The third line contains n integers b_1,b_2,...,b_n (1 ≤ b_i), indicating the characteristic b of each grid.
Then, the following n-2 lines, the (i+1)-th line contains three integers , , val_i (1 ≤ , < i, , 1 ≤ val_i), indicating the additional characteristic from the third grid to the n-th grid.

输出描述

For each test case, output the maximum beauty value of all the given grids.

示例1

输入:

3
1 2 3
3 2 1
1 2 2

输出:

6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 604K, 提交时间: 2020-06-06 14:57:09

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ls o<<1
#define rs o<<1|1
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
const double pi=acos(-1.0);
const double eps=1e-10;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int maxn=1005;
int n,tot,s,t,cnt,sum;
struct node{
	int a,b,p1,p2,v;
}buf[maxn];
struct edge{
	int to,nxt,we;
}es[100005];
int head[3005],dep[3005],cur[3005];
queue<int>q;
void init(){
	tot=0;
	memset(head,-1,sizeof(head));
}
void addEdge(int u,int v,int w){
	es[tot].to=v;
	es[tot].we=w;
	es[tot].nxt=head[u];
	head[u]=tot++;
}
void add(int u,int v,int w){
	addEdge(u,v,w);
	addEdge(v,u,0);
}
int bfs(){
	while(!q.empty())q.pop();
	for(int i=0;i<=2*n+1;i++)dep[i]=0,cur[i]=head[i];
	dep[s]=1;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=es[i].nxt){
			int v=es[i].to;
			if(es[i].we&&!dep[v]){
				dep[v]=dep[u]+1;
				if(v==t)return 1;
				q.push(v);
			}
		}
	}
	return 0;
}
int dfs(int u,int flow){
	int rflow;
	if(u==t)return flow;
	int used=0;
	for(int i=cur[u];i!=-1;i=es[i].nxt){
		cur[u]=i;
		int v=es[i].to;
		if(es[i].we&&dep[v]==dep[u]+1){
			if(rflow=dfs(v,min(es[i].we,flow-used))){
				es[i].we-=rflow;
				es[i^1].we+=rflow;
				used+=rflow;
				if(used==flow)break;
			}
			else dep[v]=0;
		}
	}
	return used;
}
int dinic(){
	int ans=0;
	while(bfs())ans+=dfs(s,INF);
	return ans;
}
int main(void){
//	freopen("in_7.txt","r",stdin);
//	freopen("out_7.txt","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&buf[i].a);
	for(int i=1;i<=n;i++)scanf("%d",&buf[i].b);
	for(int i=3;i<=n;i++){
		scanf("%d%d%d",&buf[i].p1,&buf[i].p2,&buf[i].v);
	}
	init();
	s=0;t=2*n+1;
	for(int i=1;i<=n;i++){
		add(s,i,buf[i].a);
		add(i,t,buf[i].b);
		sum+=buf[i].a+buf[i].b;
	}
	for(int i=3;i<=n;i++){
		add(i,n+i,buf[i].v);
		add(n+i,buf[i].p1,INF);
		add(n+i,buf[i].p2,INF);
	}
	int ans=dinic();
	printf("%d\n",sum-ans);
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 6ms, 内存消耗: 540K, 提交时间: 2020-08-28 16:21:29

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=4e3+10,M=1e6+10;
int n,a[N],b[N],res,s,t,h[N],cnt;
int head[N],nex[M],to[M],w[M],tot=1;
inline void ade(int a,int b,int c){to[++tot]=b; nex[tot]=head[a]; w[tot]=c; head[a]=tot;}
inline void add(int a,int b,int c){ade(a,b,c);	ade(b,a,0);}
inline int bfs(){
	queue<int> q; q.push(s); memset(h,0,sizeof h); h[s]=1;
	while(q.size()){
		int u=q.front();	q.pop();
		for(int i=head[u];i;i=nex[i]) if(!h[to[i]]&&w[i]) h[to[i]]=h[u]+1,q.push(to[i]);
	}
	return h[t];
}
int dfs(int x,int f){
	if(x==t)	return f;	int fl=0;
	for(int i=head[x];i&&f;i=nex[i]){
		if(w[i]&&h[to[i]]==h[x]+1){
			int mi=dfs(to[i],min(w[i],f));
			w[i]-=mi,w[i^1]+=mi,fl+=mi,f-=mi;
		}
	}
	if(!fl)	h[x]=-1;
	return fl;
}
inline int dinic(){
	int res=0;
	while(bfs())	res+=dfs(s,inf);
	return res;
}
signed main(){
	cin>>n; t=N-5; cnt=n;
	for(int i=1;i<=n;i++)	cin>>a[i],add(s,i,a[i]),res+=a[i];
	for(int i=1;i<=n;i++)	cin>>b[i],add(i,t,b[i]),res+=b[i];
	for(int i=3,x,y,val;i<=n;i++)
		cin>>x>>y>>val,++cnt,add(i,cnt,val),add(cnt,x,val),add(cnt,y,val);
	cout<<res-dinic();
	return 0;
}

上一题