• [Luogu4177][CEOI2008]order


    luogu

    sol

    这题有点像网络流24题里面的太空飞行计划啊。
    最大收益=总收益-最小损失。
    先令(ans=sum)任务收益。
    源点向每个任务连容量为收益的边。
    每个机器向汇点连容量为购买费用的边。
    每个任务向对应的机器连容量为租赁费用的边。
    最小割即可。

    code

    (bfs)里面用了一个玄学卡常技巧。并没有什么用

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    int gi()
    {
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    const int N = 2500;
    const int inf = 1e9;[Luogu4177][CEOI2008]order
    struct edge{int to,nxt,w;}a[N*N];
    int n,m,S,T,head[N],cnt=1,dep[N],cur[N],ans;
    queue<int>Q;
    void link(int u,int v,int w)
    {
    	a[++cnt]=(edge){v,head[u],w};
    	head[u]=cnt;
    	a[++cnt]=(edge){u,head[v],0};
    	head[v]=cnt;
    }
    bool bfs()
    {
    	memset(dep,0,sizeof(dep));dep[S]=1;
    	while (!Q.empty()) Q.pop();Q.push(S);
    	while (!Q.empty())
    	{
    		int u=Q.front();Q.pop();
    		for (int e=head[u];e;e=a[e].nxt)
    			if (a[e].w&&!dep[a[e].to])
    			{
    				dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
    				if (a[e].to==T) return true;
    			}
    	}
    	return false;
    }
    int dfs(int u,int f)
    {
    	if (u==T) return f;
    	for (int &e=cur[u];e;e=a[e].nxt)
    		if (a[e].w&&dep[a[e].to]==dep[u]+1)
    		{
    			int tmp=dfs(a[e].to,min(a[e].w,f));
    			if (tmp) {a[e].w-=tmp;a[e^1].w+=tmp;return tmp;}
    		}
    	return 0;
    }
    int Dinic()
    {
    	int res=0;
    	while (bfs())
    	{
    		for (int i=1;i<=T;++i) cur[i]=head[i];
    		while (int tmp=dfs(S,inf)) res+=tmp;
    	}
    	return res;
    }
    int main()
    {
    	n=gi();m=gi();S=n+m+1;T=S+1;
    	for (int i=1;i<=n;++i)
    	{
    		int val=gi(),k=gi();
    		link(S,i,val);ans+=val;
    		while (k--)
    		{
    			int x=gi(),y=gi();
    			link(i,n+x,y);
    		}
    	}
    	for (int i=1,val;i<=m;++i) val=gi(),link(n+i,T,val);
    	printf("%d
    ",ans-Dinic());
    	return 0;
    }
    
  • 相关阅读:
    第八周总结
    第五周学习总结&实验报告(三)
    第四周课程总结&试验报告(二)
    第三周课程总结&实验报告(一)
    第二周Java学习总结
    2019春学习总结
    第二周基础学习
    第三周编程总结
    2019春第四次课程设计实验报告
    2019春第三次课程设计实验报告
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8711006.html
Copyright © 2020-2023  润新知