zoukankan      html  css  js  c++  java
  • luogu p4174 最大获利(最大权闭合子图)

    luogu p4174 最大获利(最大权闭合子图)

    给定n个点,m条边,每条边有一个贡献,每个点有一个代价。选择一条边,会付出边所连两个点的代价,问最大代价。

    我们换个建图方式:把图G中的边(e_i)也建成G‘中的点(E_i)。由于在G中,选择了一条边(e_i)必须选择边旁的点(V_{i_1})(V_{i2}),所以我们在G‘中,将(E_i)连两条向(V_{i1})(V_{i2})的有向边。对G’求最大权子图即可。

    #include <cstdio> 
    #include <cstring>
    using namespace std;
    
    const int maxn=1e5+5, maxm=2e5+5, INF=1e9;
    int n, m, src, dst, ans;
    inline int min(int x, int y){ return x<y?x:y; }
    
    struct Edge{
    	int to, nxt, f;
    }e[maxm*2];
    int fir[maxn], cnte=1;
    void addedge(int x, int y, int v){
    	Edge &ed=e[++cnte];
    	ed.to=y; ed.nxt=fir[x]; ed.f=v; fir[x]=cnte; 
    }
    
    int q[maxn], head, tail, dep[maxn];
    bool bfs(){
    	memset(dep, 0, sizeof(dep)); dep[src]=1;
    	head=tail=0; q[tail++]=src; int u;
    	while (head<tail){
    		u=q[head++];
    		for (int i=fir[u]; ~i; i=e[i].nxt)
    			if (e[i].f&&!dep[e[i].to]){
    				dep[e[i].to]=dep[u]+1;
    				q[tail++]=e[i].to;
    			}
    	}
    	return dep[dst];
    }
    
    int cur[maxn];
    int dfs(int u, int flow){
    	if (u==dst) return flow;
    	for (int i=cur[u]; ~i; i=e[i].nxt, cur[u]=i)
    	if (dep[e[i].to]==dep[u]+1&&e[i].f){
    		int minm=dfs(e[i].to, min(flow, e[i].f));
    		e[i].f-=minm; e[i^1].f+=minm;
    		if (minm) return minm;
    	}
    	return 0;
    }
    
    int Dinic(){
    	int ans=0, t;
    	while (bfs()){
    		memcpy(cur, fir, sizeof(fir));
    		while (t=dfs(src, INF)) ans+=t; 
    	}
    	return ans;
    }
    
    int main(){
    	memset(fir, -1, sizeof(fir));
    	scanf("%d%d", &n, &m); int t, ta, tb;
    	src=0; dst=n+m+1;
    	for (int i=1; i<=n; ++i){
    		scanf("%d", &t);
    		addedge(src, i, t); addedge(i, src, 0);
    	}
    	for (int i=1; i<=m; ++i){
    		scanf("%d%d%d", &ta, &tb, &t); ans+=t;
    		addedge(ta, i+n, INF); addedge(i+n, ta, 0);
    		addedge(tb, i+n, INF); addedge(i+n, tb, 0);
    		addedge(i+n, dst, t); addedge(dst, i+n, 0);
    	}
    	ans-=Dinic();
    	printf("%d
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    UVA 10003:Cutting Sticks 区间DP
    UVAlive 10154:Dire Wolf 区间DP
    HDU 5071:Chat(2014 Asia AnShan Regional Contest)
    HDU 5074:Hatsune Miku(2014 Asia AnShan Regional Contest)
    android 代码混淆及问题大集锦
    android调试bug集锦 onActivityResult立即返回,并且被CANCEL
    开启g++ 编辑器 c++11特性
    解析最简单的验证码
    将图片文件转换为.py文件
    使用pyinstaller 2.1将python打包并添加版本信息和图标
  • 原文地址:https://www.cnblogs.com/MyNameIsPc/p/9162256.html
Copyright © 2011-2022 走看看