link:https://www.luogu.org/problemnew/show/P2762
题意
承担实验赚钱,但是要花去对应仪器的费用,仪器可能共用。
求最大的收益和对应的选择方案。
思路
这道题读入有点技巧,就是要自己判断换行
这道题和费用流关系不大,是最大权闭合子图,源点连接实验,容量为收益,实验向对应的仪器连接容量为inf的边,仪器向汇点连接容量为费用的边。
跑出最小割s,即最大流,然后用实验总收益 - s即可。
至于如何输出方案,即输出最大权闭合子图中的点。就是跑最后一次bfs的时候,有dis的点就是选中的点。
结合图形我们可以这么理解,从源点流到实验的边一定不能流满,要是满了,就说明这个做这个实验没有任何意义。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e4+9; struct E{ int u,v,w; int nxt; }edge[maxn]; int gtot = 0,head[maxn]; void addedge(int u,int v,int w){ edge[gtot].u = u; edge[gtot].v = v; edge[gtot].w = w; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].u = v; edge[gtot].v = u; edge[gtot].w = 0; edge[gtot].nxt = head[v]; head[v] = gtot++; } int dis[maxn],cur[maxn]; bool bfs(int s,int t){ rep(i, s, t) cur[i] = head[i]; memset(dis, inf, sizeof(dis)); dis[s] = 0; queue<int>que; que.push(s); while(!que.empty()){ int u = que.front(); que.pop(); for(int i = head[u]; ~i; i = edge[i].nxt){ int v = edge[i].v, w = edge[i].w; if(w > 0 && dis[v] > dis[u] + 1){ dis[v] = dis[u] + 1; que.push(v); } } } return dis[t] < inf; } int dfs(int u,int t,int maxflow){ if(u == t || maxflow == 0 ) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){ cur[u] = i; int v = edge[i].v, w = edge[i].w; if(w > 0 && dis[v] == dis[u] + 1) { int f = dfs(v, t, min(w, maxflow)); if(f > 0) { edge[i].w -= f; edge[i^1].w += f; return f; } } } return 0; } int dinic(int s,int t){ int flow = 0; while(bfs(s, t)){ while(int f = dfs(s, t, inf)) flow += f; } return flow; } char tools[10000]; int init[maxn]; int main(){ memset(head,-1, sizeof(head)); int n,m; scanf("%d%d", &n, &m); int s = 0, t = n+m+1; int sum = 0; for(int i=1; i<=n; i++) { int x; scanf("%d", &x); addedge(s, i, x); init[i] = x; sum += x; memset(tools,0,sizeof tools); cin.getline(tools,10000); int ulen=0,tool; while (sscanf(tools+ulen,"%d",&tool)==1)//之前已经用scanf读完了赞助商同意支付该实验的费用 {//tool是该实验所需仪器的其中一个 //这一行,你可以将读进来的编号进行储存、处理,如连边。 addedge(i, n+tool,inf); // cout<<tool<<endl; if (tool==0) ulen++; else { while (tool) { tool/=10; ulen++; } } ulen++; } } for(int i=1; i<=m; i++){ int x; scanf("%d", &x); addedge(i+n, t, x); init[i+n] = x; } int ans = sum - dinic(s, t); for(int i=1; i <= n; i++) { if(dis[i] < inf) printf("%d ", i); } puts(""); for(int i=1; i<=m; i++){ if(dis[i+n] < inf) printf("%d ", i); } puts(""); printf("%d ", ans); return 0; }