題目大意:給出 n 個卡片,可以自由買賣,且價格都是相同的,再給出 m 個集合,如果已經得到了其中一個集合中的卡片,那么可以獲得該集合的收益,問如何操作可以使得收益最大化
題目分析:最大權閉合子圖的模板題。。感覺虧死了,訓練的時候三個英語不好的人根本不會主動去讀題,只能被動跟著榜慢慢耗,結果這樣一道本應該被秒掉的題目只能賽后看別人翻譯的題意再補了
相對于傳統的模型,多了一項就是卡片也是可以售出的,又因為售出和購買的價格都是相同的,所以不妨假設初始時就將所有的卡片售出,這樣問題就轉換成模板題了。。建圖方式如下
st -> 每個卡片,權值為對應的 val每個卡片 -> 對應的集合,權值為 inf每個集合 -> ed,權值為對應的 val
最后答案就是:初始時已有卡片的權值 + 所有集合的權值 - 最小割 了
稍微需要注意的是,第三個樣例錯了,第一個集合應該是 1 2 3 4 的,少打了一個 4
代碼:
?
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=110;struct Edge
{int to,w,next;
}edge[N*N];//邊數int head[N],cnt;void addedge(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向邊邊權設置為0edge[cnt].next=head[v];head[v]=cnt++;
}int d[N],now[N];//深度 當前弧優化bool bfs(int s,int t)//尋找增廣路
{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;
}int dinic(int x,int t,int flow)//更新答案
{if(x==t)return flow;int rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(w&&d[v]==d[x]+1){int k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;
}void init()
{memset(now,0,sizeof(now));memset(head,-1,sizeof(head));cnt=0;
}int solve(int st,int ed)
{int ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);init();int n,st=N-1,ed=st-1;scanf("%d",&n);int sum=0;for(int i=1;i<=n;i++){int val,s;scanf("%d%d",&val,&s);sum+=val*s;addedge(st,i,val);}int m;scanf("%d",&m);for(int i=1;i<=m;i++){int num,val;scanf("%d%d",&num,&val);sum+=val;addedge(i+n,ed,val);while(num--){int x;scanf("%d",&x);addedge(x,i+n,inf);}}printf("%d\n",sum-solve(st,ed));return 0;
}
?
總結
以上是生活随笔為你收集整理的中石油训练赛 - Trading Cards(最大权闭合子图)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。