zoukankan      html  css  js  c++  java
  • uva 10330

    uva 10330 - Power Transmission


    题目大意:最大流问题。


    解题思路:増广路算法。


    #include <stdio.h>
    #include <string.h>
    #include <queue>
    
    using namespace std;
    
    #define min(a,b) (a)<(b)?(a):(b)
    
    const int N = 105;
    const int INF = 0x3f3f3f3f;
    
    int n, s[N], g[N][N], f[N][N];
    
    void init() {
    	memset(s, 0, sizeof(s));
    	memset(g, INF, sizeof(g));
    	memset(f, INF, sizeof(f));
    
    	for (int i = 1; i <= n; i++)
    		scanf("%d", &s[i]);
    
    	int a, b, c, t;
    	scanf("%d", &t);
    	for (int i = 0; i < t; i++) {
    		scanf("%d%d%d", &a, &b, &c);
    		g[a][b] = min(g[a][b], c);
    		f[a][b] = 0;
    	}
    
    	scanf("%d%d", &a, &b);
    	for (int i = 0; i < a; i++) {
    		scanf("%d", &c);
    		g[0][c]--;
    		f[0][c] = 0;
    	}
    
    	for (int i = 0; i < b; i++) {
    		scanf("%d", &c);
    		g[c][n + 1]--;
    		f[c][n + 1] = 0;
    	}
    }
    
    int solve() {
    	n++;
    	s[n] = INF;
    	int a[N], vis[N];
    	int ans = 0;
    	queue<int> q;
    
    	while (1) {
    
    		memset(vis, 0, sizeof(vis));
    		memset(a, 0, sizeof(a));
    
    		int c = 0, t;
    
    		vis[c] = 0;
    		a[c] = INF;
    		q.push(c);
    
    		while (!q.empty()) {
    
    			c = q.front(), q.pop();
    
    			for (int i = 1; i <= n; i++) {
    
    				if (g[c][i] == INF) continue;
    				t = min(g[c][i] - f[c][i], min(a[c], s[i]));
    
    				if (t > a[i]) {
    					vis[i] = c;
    					a[i] = t;
    					q.push(i);
    				}
    			}
    		}
    
    		if (a[n] == 0) break;
    		ans += a[n];
    
    		for (int i = n; i; i = vis[i]) {
    
    			s[i] -= a[n];
    			f[vis[i]][i] += a[n];
    		}
    	}
    	return ans;
    }
    
    int main () {
    
    	while (scanf("%d", &n) == 1) {
    
    		init();
    		printf("%d
    ", solve());
    	}
    	return 0;
    }
    


  • 相关阅读:
    数据库连接
    TUniConnection连接
    在Bootstrap中得模态框(modal)中下拉不能显示得问题
    git ---匿名分支和checkout命令
    git ---合并和删除分支
    Git ---创建和切换分支
    git --删除文件、重命名
    git --版本对比
    git ---回到过去
    git ---查看工作状态和历史提交
  • 原文地址:https://www.cnblogs.com/riasky/p/3433222.html
Copyright © 2011-2022 走看看