POJ - 1698 Alice's Chance(二分图多重匹配-网络流)
生活随笔
收集整理的這篇文章主要介紹了
POJ - 1698 Alice's Chance(二分图多重匹配-网络流)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:愛麗絲要拍電影,現在有n部電影,每部電影都只能在每周特定的日期拍攝,并且需要拍攝d天,要在w周之內完成,現在問愛麗絲能否將所有的電影都拍攝完成
題目分析:因為每部電影可以對應多個星期,所以把模型抽象出來就是個二分圖最大匹配問題,用網絡流直接掛模板就能解決了,現在我們來分析一下如何建邊
因為w的值是不確定的,所以我們需要將星期拆點,也就是將w周拆成w*7天,然后超級源點s連接每一部電影,權值為d,每部電影與可行日期建邊,權值為1,最后所有日期與超級匯點t建邊,權值為1,跑一下最大流判斷一下答案是否等于所有d之和即可,為了方便操作,這里我設:
如此建邊然后套模板就好了
代碼:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=500;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*N];//深度 當前弧優(yōu)化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(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() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){init();int n;scanf("%d",&n);int s=0,t=N-1;int target=0;//目標天數 int tw=0;for(int i=1;i<=n;i++)//電影數 {int f[10];for(int j=1;j<=7;j++)scanf("%d",f+j);int d,w;scanf("%d%d",&d,&w);tw=max(tw,w);//實時更新最大周 target+=d;for(int j=0;j<w;j++)//周for(int k=1;k<=7;k++)//星期if(f[k]) addedge(i,n+j*7+k,1);//電影->可行天 addedge(s,i,d);//源點->電影 }for(int j=n+1;j<=n+tw*7;j++)addedge(j,t,1);//每一天->匯點int ans=solve(s,t);if(ans==target)printf("Yes\n");elseprintf("No\n");}return 0; }?
總結
以上是生活随笔為你收集整理的POJ - 1698 Alice's Chance(二分图多重匹配-网络流)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1255D F
- 下一篇: POJ - 3179 Corral th