zoukankan      html  css  js  c++  java
  • HDU 3215 Being a Hero(最小割)

    HDU 3215 Being a Hero

    题目链接

    题意:一个英雄,分到几个城市,每一个城市有一个价值,可是要求分到城市后,必须破坏掉道路使得首都1都不能到达,破坏道路有开销。问最大能获得的收益和须要破坏的道路ID

    思路:最小割,城市1做源点。有向边建图,容量为代价,然后每一个能够分的城市连到汇点。容量为价值,跑一下最小割就可以

    代码:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    const int MAXNODE = 1005;
    const int MAXEDGE = 300005;
    
    typedef int Type;
    const Type INF = 0x3f3f3f3f;
    
    struct Edge {
    	int u, v, id;
    	Type cap, flow;
    	Edge() {}
    	Edge(int u, int v, Type cap, Type flow, int id) {
    		this->u = u;
    		this->v = v;
    		this->cap = cap;
    		this->flow = flow;
    		this->id = id;
    	}
    };
    
    struct Dinic {
    	int n, m, s, t;
    	Edge edges[MAXEDGE];
    	int first[MAXNODE];
    	int next[MAXEDGE];
    	bool vis[MAXNODE];
    	Type d[MAXNODE];
    	int cur[MAXNODE];
    	vector<int> cut;
    
    	void init(int n) {
    		this->n = n;
    		memset(first, -1, sizeof(first));
    		m = 0;
    	}
    	void add_Edge(int u, int v, Type cap, int id) {
    		edges[m] = Edge(u, v, cap, 0, id);
    		next[m] = first[u];
    		first[u] = m++;
    		edges[m] = Edge(v, u, 0, 0, id);
    		next[m] = first[v];
    		first[v] = m++;
    	}
    
    	bool bfs() {
    		memset(vis, false, sizeof(vis));
    		queue<int> Q;
    		Q.push(s);
    		d[s] = 0;
    		vis[s] = true;
    		while (!Q.empty()) {
    			int u = Q.front(); Q.pop();
    			for (int i = first[u]; i != -1; i = next[i]) {
    				Edge& e = edges[i];
    				if (!vis[e.v] && e.cap > e.flow) {
    					vis[e.v] = true;
    					d[e.v] = d[u] + 1;
    					Q.push(e.v);
    				}
    			}
    		}
    		return vis[t];
    	}
    
    	Type dfs(int u, Type a) {
    		if (u == t || a == 0) return a;
    		Type flow = 0, f;
    		for (int &i = cur[u]; i != -1; i = next[i]) {
    			Edge& e = edges[i];
    			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
    				e.flow += f;
    				edges[i^1].flow -= f;
    				flow += f;
    				a -= f;
    				if (a == 0) break;
    			}
    		}
    		return flow;
    	}
    
    	Type Maxflow(int s, int t) {
    		this->s = s; this->t = t;
    		Type flow = 0;
    		while (bfs()) {
    			for (int i = 0; i < n; i++)
    				cur[i] = first[i];
    			flow += dfs(s, INF);
    		}
    		return flow;
    	}
    
    	void MinCut() {
    		cut.clear();
    		for (int i = 0; i < m; i += 2) {
    			if (vis[edges[i].u] && !vis[edges[i].v] && edges[i].id)
    				cut.push_back(i);
    		}
    		int sz = cut.size();
    		printf("%d", sz);
    		for (int i = 0; i < sz; i++)
    			printf(" %d", edges[cut[i]].id);
    		printf("
    ");
    	}
    } gao;
    
    int t, n, m, f;
    
    int main() {
    	int cas = 0;
    	scanf("%d", &t);
    	while (t--) {
    		scanf("%d%d%d", &n, &m, &f);
    		gao.init(n + 1);
    		int u, v, w, tot = 0;
    		for (int i = 1; i <= m; i++) {
    			scanf("%d%d%d", &u, &v, &w);
    			gao.add_Edge(u, v, w, i);
    		}
    		while (f--) {
    			scanf("%d%d", &u, &w);
    			tot += w;
    			gao.add_Edge(u, 0, w, 0);
    		}
    		printf("Case %d: %d
    ", ++cas, tot - gao.Maxflow(1, 0));
    		gao.MinCut();
    	}
    	return 0;
    }


  • 相关阅读:
    uninstall_edge:win10带浏览器edge卸载工具
    安装spacedesk后,Win10状态栏图标间距变宽
    jacob实现语音朗读一段文本
    汇编语言-12内中断
    告警只提示一次,未解决也不再次提示
    汇编语言-11标志寄存器
    第2章 顺序表及其顺序存储
    第1章 概论
    再见:计算机行业
    QPainter
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7028840.html
Copyright © 2011-2022 走看看