标签:while fir sum getline ons add getch 含义 ack
大意: $m$个实验, 每个实验只能进行一次, 第$i$实验需要的仪器集合$R_i$, 收益$p_i$. $n$种仪器, 第$i$种仪器花费$c_i$, 每种仪器可以多次使用. 求最大收益.
数据范围$n,m\le 50$
最大权闭合子图.
源点$S$向第$i$个实验连边权为$p_i$, 第$i$个实验向集合$R_i$中的仪器连边权$INF$, 仪器$i$向汇点$T$连边权$c_i$. 则$\sum p_i$减去$S->T$的最小割就为最大收益.
对于实验与$S$间的割的含义为不选这个实验, 对于仪器与$T$之间的割的含义为选这个仪器.
跑完最大流后仍与$S$连通的点即为最优方案.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #include <unordered_map> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘\n‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘ ‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head #ifdef ONLINE_JUDGE const int N = 1e6+10; #else const int N = 999; #endif int n, m, S, T; struct edge { int v,w,next; } e[N]; int head[N], dep[N], vis[N], cur[N], cnt=1; queue<int> Q; void add(int u, int v, int w) { e[++cnt] = {v,w,head[u]}; head[u] = cnt; e[++cnt] = {u,0,head[v]}; head[v] = cnt; } int bfs() { REP(i,1,T) dep[i]=INF,vis[i]=0,cur[i]=head[i]; dep[S]=0,Q.push(S); while (Q.size()) { int u = Q.front(); Q.pop(); for (int i=head[u]; i; i=e[i].next) { if (dep[e[i].v]>dep[u]+1&&e[i].w) { dep[e[i].v]=dep[u]+1; Q.push(e[i].v); } } } return dep[T]!=INF; } int dfs(int x, int w) { if (x==T) return w; int used = 0; for (int i=cur[x]; i; i=e[i].next) { cur[x] = i; if (dep[e[i].v]==dep[x]+1&&e[i].w) { int flow = dfs(e[i].v,min(w-used,e[i].w)); if (flow) { used += flow; e[i].w -= flow; e[i^1].w += flow; if (used==w) break; } } } return used; } int dinic() { int ans = 0; while (bfs()) ans+=dfs(S,INF); return ans; } int main() { cin>>m>>n; cin.ignore(); S = n+m+1, T = S+1; int sum = 0; REP(i,1,m) { string s; getline(cin,s); stringstream ss(s); int k, p; ss>>p; add(S,i,p); sum += p; while (ss>>k) add(i,k+m,INF); } REP(i,1,n) { int r; cin>>r; add(i+m,T,r); } int x = dinic(); REP(i,1,m) if (dep[i]!=INF) printf("%d ",i);hr; REP(i,m+1,n+m) if (dep[i]!=INF) printf("%d ",i-m);hr; printf("%d\n", sum-x); }
标签:while fir sum getline ons add getch 含义 ack
原文地址:https://www.cnblogs.com/uid001/p/10987098.html