NC205330. GridPainting
描述
输入描述
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 (1 ≤ ≤ ), indicating the characteristic a of each grid.
The third line contains n integers (1 ≤ ≤ ), indicating the characteristic b of each grid.
Then, the following n-2 lines, the (i+1)-th line contains three integers , , (1 ≤ , < i, ≠ , 1 ≤ ≤ ), 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; }