UVALive - 3231 Fair Share(最大流+二分)
生活随笔
收集整理的這篇文章主要介紹了
UVALive - 3231 Fair Share(最大流+二分)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出n個處理器和m個任務(wù),每個任務(wù)給出可以運行的兩個處理器,只需要其中一個處理器完成即可, 問如何分配處理方案,能使得n個處理器中處理任務(wù)最多的處理器所處理的任務(wù)最少
題目分析:求最大值的最小值,典型的二分,只不過套了個網(wǎng)絡(luò)流,二分枚舉答案,建圖跑網(wǎng)絡(luò)流根據(jù)是否滿流來判斷是否滿足條件即可
代碼:
#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=2e4+100;struct Edge {int to,w,next; }edge[N*4];//邊數(shù)int n,m,head[N],cnt;struct Node {int a,b;void input(){scanf("%d%d",&a,&b);} }a[N];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;//反向邊邊權(quán)設(shè)置為0edge[cnt].next=head[v];head[v]=cnt++; }int d[N],now[N];//深度 當(dāng)前弧優(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(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; }bool check(int x) {init();int st=N-1,ed=st-1;for(int i=1;i<=n;i++)//處理器 addedge(st,i,x);for(int i=1;i<=m;i++)//任務(wù) {addedge(i+n,ed,1);addedge(a[i].a,i+n,inf);addedge(a[i].b,i+n,inf);}return solve(st,ed)==m; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){scanf("%d%d",&n,&m);for(int i=1;i<=m;i++)a[i].input();int l=0,r=inf,ans;while(l<=r){int mid=l+r>>1;if(check(mid)){ans=mid;r=mid-1;}elsel=mid+1;}printf("%d\n",ans);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的UVALive - 3231 Fair Share(最大流+二分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ - 2175 Evacuatio
- 下一篇: 2019ICPC(银川) - Deliv