bzoj 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+最大流

版权声明:本文为博主原创文章,转载请联系博主。 https://blog.csdn.net/everlasting_20141622/article/details/77231446

题目链接


二分答案,每次把边长小于mid的边放到图中,正反的边容量都为1,跑最大流

然后用最大流和T判断一下,如果大于T返回true,否则false


代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
 
#define ll long long
#define inf 10000001
 
using namespace std;
 
struct node{
    int to,num;
}edge[80080];

int fro[80080],to[80080],len[80080];
int n,m,T;
int pn=1;
vector<int>v[220];
int edgenum=0;
int d[220];
 
void link(int x,int y,int z){
    node t;
    t.to=y;
    t.num=z;
    edge[edgenum]=t;
    v[x].push_back(edgenum);
    edgenum++;
    t.to=x;
    t.num=z;
    edge[edgenum]=t;
    v[y].push_back(edgenum);
    edgenum++;
} 
 
bool bfs(){
    memset(d,0,sizeof(d));
    d[1]=1;
    queue<int>q;
    q.push(1);
    while(!q.empty()){
        int t=q.front();
        q.pop();
        if(t==pn)continue;
        for(int i=0; i<v[t].size(); i++){
            int x=v[t][i];
            int to=edge[x].to;
            int num=edge[x].num;
            if(num==0 || d[to]!=0)continue;
            d[to]=d[t]+1;
            q.push(to);
        }
    }
    if(d[pn]!=0)return true;
    return false;
}
 
ll dfs(int x,int y){
    if(x==pn || y==0)return y;
    ll sum=0;
    for(int i=0; i<v[x].size(); i++){
        int j=v[x][i];
        int to=edge[j].to;
        int num=edge[j].num;
        if(num){
            if(d[to]!=d[x]+1)continue;
            ll k=dfs(to,min(num,y));
            y-=k;
            sum+=k;
            edge[j].num-=k;
            edge[j^1].num+=k;
            if(y==0)break;
        }
    }
    if(sum==0)d[x]=0;
    return sum;
}
 

bool check(int x){
	long long ans=0;
	edgenum=0;
	for(int i=1; i<=n; i++)v[i].clear();
	for(int i=1; i<=m; i++){
		if(len[i]<=x){
			link(fro[i],to[i],1);
		}
	}
	while(bfs())ans+=dfs(1,inf);
	return ans>=T;
}

int main(){
    scanf("%d%d%d",&n,&m,&T);
    for(int i=1; i<=m; i++){
    	scanf("%d%d%d",&fro[i],&to[i],&len[i]);
    }
    pn=n;
    int l=1,r=1000000;
    int ans;
    while(l<=r){
    	int mid=(l+r)/2;
    	if(check(mid))ans=mid,r=mid-1;
    	else l=mid+1;
    }
    printf("%d\n",ans);
     
    return 0;
}


没有更多推荐了,返回首页