[网络流24题]太空飞行计划
生活随笔
收集整理的這篇文章主要介紹了
[网络流24题]太空飞行计划
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
分析
最大權閉合子圖模板題
把實驗獲得的收益看成正權點,儀器費用看成負權點,每個實驗向所需要的儀器連邊
跑最大權閉合子圖即可
詳見https://www.cnblogs.com/birchtree/p/10304793.html
代碼
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #define maxn 110 #define maxm 10005 #define INF 0x7fffffff using namespace std; int n,m; int p[maxn]; int c[maxn]; struct edge{int from;int to;int next; }E[maxm<<1]; long long flow[maxm]; int head[maxn]; int sz=1; void add_edge(int u,int v,int w){ // printf("%d->%d %d\n",u,v,w);sz++;E[sz].from=u;E[sz].to=v;E[sz].next=head[u];head[u]=sz;flow[sz]=w; }int deep[maxn]; int bfs(int s,int t){queue<int>q;memset(deep,0,sizeof(deep));deep[s]=1;q.push(s);while(!q.empty()){int x=q.front();q.pop();for(int i=head[x];i;i=E[i].next){int y=E[i].to;if(flow[i]&&!deep[y]){deep[y]=deep[x]+1;q.push(y);if(y==t) return 1;} }}return 0; }long long dfs(int x,int t,long long minf){if(x==t) return minf;long long rest=minf,k;for(int i=head[x];i;i=E[i].next){int y=E[i].to;if(flow[i]&&deep[y]==deep[x]+1){k=dfs(y,t,min(rest,flow[i]));if(k==0) deep[y]=0;flow[i]-=k;flow[i^1]+=k;rest-=k;}}return minf-rest; }long long dinic(int s,int t){long long maxflow,nowflow;maxflow=0;while(bfs(s,t)){while(nowflow=dfs(s,t,INF)){maxflow+=nowflow;}}return maxflow; }vector<int>ans; int main(){int x;char ch;long long tot=0;scanf("%d %d",&m,&n);for(int i=1;i<=m;i++){scanf("%d",&p[i]);tot+=p[i];while(scanf("%d%c",&x,&ch)!=EOF){add_edge(i,x+m,INF);add_edge(x+m,i,0);if(ch=='\n'||ch=='\r') break;}}for(int i=1;i<=n;i++) scanf("%d",&c[i]);for(int i=1;i<=m;i++){add_edge(0,i,p[i]);add_edge(i,0,0);} for(int i=1;i<=n;i++){add_edge(i+m,m+n+1,c[i]);add_edge(m+n+1,i+m,0);}long long res=tot-dinic(0,m+n+1);for(int i=1;i<=m;i++){if(deep[i]!=0){ans.push_back(i);}}for(int i=0;i<ans.size();i++){printf("%d",ans[i]);if(i!=ans.size()-1) printf(" ");else printf("\n");}ans.clear();for(int i=m+1;i<=m+n;i++){if(deep[i]!=0){ans.push_back(i-m);}}for(int i=0;i<ans.size();i++){printf("%d",ans[i]);if(i!=ans.size()-1) printf(" ");else printf("\n");}printf("%lld\n",res); }轉載于:https://www.cnblogs.com/birchtree/p/10430859.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的[网络流24题]太空飞行计划的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: react+ant design Bre
- 下一篇: css : 使用浮动实现左右各放一个元