• 运输问题 费用流


    和luogu的分配问题一样,建边也非常简单,基本和[分配问题]一模一样;

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<time.h>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize(2)
    using namespace std;
    #define maxn 20005
    #define inf 0x7fffffff
    //#define INF 1e18
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    #define mclr(x,a) memset((x),a,sizeof(x))
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-5
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    
    inline int rd() {
    	int x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    int sqr(int x) { return x * x; }
    
    
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    bool vis[maxn];
    int n, m, s, t;
    int x, y, f, z;
    int dis[maxn], pre[maxn], last[maxn], flow[maxn];
    int maxflow, mincost;
    
    struct node {
    	int to, nxt, flow, dis;
    }edge[maxn << 2];
    
    int head[maxn], cnt;
    
    
    void addedge(int from, int to, int flow, int dis) {
    	edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].dis = dis;
    	edge[cnt].nxt = head[from]; head[from] = cnt;
    }
    
    bool spfa1(int s, int t) {
    	memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow));
    	ms(vis);queue<int>q;
    	q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1;
    	while (!q.empty()) {
    		int now = q.front(); q.pop(); vis[now] = 0;
    		for (int i = head[now]; i != -1; i = edge[i].nxt) {
    			if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) {
    				dis[edge[i].to] = edge[i].dis + dis[now];
    				pre[edge[i].to] = now; last[edge[i].to] = i;
    				flow[edge[i].to] = min(flow[now], edge[i].flow);
    				if (!vis[edge[i].to]) {
    					vis[edge[i].to] = 1; q.push(edge[i].to);
    				}
    			}
    		}
    	}
    	return pre[t] != -1;
    }
    
    bool spfa2(int s, int t) {
    	memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow));
    	ms(vis); queue<int>q;
    	q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1;
    	while (!q.empty()) {
    		int now = q.front(); q.pop(); vis[now] = 0;
    		for (int i = head[now]; i != -1; i = edge[i].nxt) {
    			if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) {
    				dis[edge[i].to] = edge[i].dis + dis[now];
    				pre[edge[i].to] = now; last[edge[i].to] = i;
    				flow[edge[i].to] = min(flow[now], edge[i].flow);
    				if (!vis[edge[i].to]) {
    					vis[edge[i].to] = 1; q.push(edge[i].to);
    				}
    			}
    		}
    	}
    	return pre[t] != -1;
    }
    
    void mincost_maxflow() {
    	while (spfa1(s, t)) {
    		int now = t;
    		maxflow += flow[t]; mincost += flow[t] * dis[t];
    		while (now != s) {
    			edge[last[now]].flow -= flow[t];
    			edge[last[now] ^ 1].flow += flow[t];
    			now = pre[now];
    		}
    	}
    }
    
    void maxcost_maxflow() {
    	while (spfa2(s, t)) {
    		int now = t;
    		maxflow += flow[t]; mincost += flow[t] * dis[t];
    		while (now != s) {
    			edge[last[now]].flow -= flow[t];
    			edge[last[now] ^ 1].flow += flow[t];
    			now = pre[now];
    		}
    	}
    }
    int A[maxn], B[maxn];
    int C[102][102];
    int main()
    {
    	//	ios::sync_with_stdio(0);
    	mclr(head, -1); cnt = 1;
    	m = rd(); n = rd();
    	for (int i = 1; i <= m; i++)A[i] = rd();
    	for (int i = 1; i <= n; i++)B[i] = rd();
    	for (int i = 1; i <= m; i++) {
    		for (int j = 1; j <= n; j++)C[i][j] = rd();
    	}
    	int ans1 = 0, ans2 = 0;
    	s = 0; t = n + m + 2;
    	for (int i = 1; i <= m; i++) {
    		addedge(s, i, A[i], 0); addedge(i, s, 0, 0);
    	}
    	for (int i = 1; i <= n; i++) {
    		addedge(i + m, t, B[i], 0); addedge(t, i + m, 0, 0);
    	}
    	for (int i = 1; i <= m; i++) {
    		for (int j = 1; j <= n; j++) {
    			addedge(i, j + m, A[i], C[i][j]); addedge(j + m, i, 0, -C[i][j]);
    		}
    	}
    	mincost_maxflow(); ans1 = mincost;
    	mclr(head, -1); cnt = 1;
    	ms(edge); ms(pre); ms(last); mincost = maxflow = 0;
    	for (int i = 1; i <= m; i++) {
    		addedge(s, i, A[i], 0); addedge(i, s, 0, 0);
    	}
    	for (int i = 1; i <= n; i++) {
    		addedge(i + m, t, B[i], 0); addedge(t, i + m, 0, 0);
    	}
    	for (int i = 1; i <= m; i++) {
    		for (int j = 1; j <= n; j++) {
    			addedge(i, j + m, A[i], -C[i][j]); addedge(j + m, i, 0, C[i][j]);
    		}
    	}
    	maxcost_maxflow(); ans2 = -mincost;
    	cout << ans1 << endl; cout << ans2 << endl;
    	return 0;
    }
    
  • 相关阅读:
    ecshop php商城系统数据库结构及表的介绍分析
    laravel clone后需要做的操作
    十几万条数据的表中,基于帝国cms 。自己亲身体验三种批量更新数据的方法,每一种的速度是什么样的
    PHP 实现过滤参数字符的方法
    Antd Modal 可拖拽移动
    golang /js index 转换excel字母表头
    React 父组件调用子组件的方法
    React ant table 用 XLSX 导出excel文件
    css3 做出顶边倾斜的 梯形 div
    Windows10 office 点击链接提示您的组策略阻止我们为您完成此操作。设置ChromeHTML也无效.
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10384420.html
Copyright © 2020-2023  润新知