zoukankan      html  css  js  c++  java
  • [Luogu4174][NOI2006]最大获益

    luogu

    sol

    一周没摸键盘了回来刷刷水题练练手感
    显然,最大化收益可以转化为最小化损失,从而建立最小割模型。
    (tot=sum_{i=1}^{m}C_i),事先假设所有的获益都得到了,那么“某一个获益没有得到”和“建立了某一个通信中转站”都被视作是损失。

    建图:
    源点向所有中转站连容量(P_i)的边。
    所有用户群向汇点连容量为(C_i)的边。
    对于一个用户群信息(A_i,B_i),从(A_i)中转站和(B_i)中转站分别向这个用户群连容量(inf)的边。
    答案就是(tot-最小割=tot-最大流)

    code

    #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 = 1e5+5;
    const int inf = 1e9;
    struct edge{int to,nxt,w;}a[N<<5];
    int n,m,s,t,head[N],cnt=1,dep[N],cur[N],tot;
    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;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);
    	}
    	return dep[t];
    }
    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 temp=dfs(a[e].to,min(a[e].w,f));
    			if (temp) {a[e].w-=temp;a[e^1].w+=temp;return temp;}
    		}
    	return 0;
    }
    int dinic()
    {
    	int res=0;
    	while (bfs())
    	{
    		for (int i=1;i<=t;++i) cur[i]=head[i];
    		while (int temp=dfs(s,inf)) res+=temp;
    	}
    	return res;
    }
    int main()
    {
    	n=gi();m=gi();s=n+m+1;t=s+1;
    	for (int i=1,w;i<=n;++i)
    		w=gi(),link(s,i,w);
    	for (int i=1,u,v,w;i<=m;++i)
    	{
    		u=gi();v=gi();w=gi();
    		link(u,i+n,inf);link(v,i+n,inf);
    		link(i+n,t,w);tot+=w;
    	}
    	printf("%d
    ",tot-dinic());
    	return 0;
    }
    
  • 相关阅读:
    小程序文档
    display: flex;
    时间戳格式化
    transition-分栏按钮动画
    animation与transition区别
    放大镜
    原生js实现瀑布流效果
    Javascript获取数组中最大和最小值
    scss基础
    C/C++ XMPP/Jabber 客户端类库对比/点评 (转)
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8544743.html
Copyright © 2011-2022 走看看