费用流 zoj3885 The Exchange of Items

传送门:点击打开链接

题意:有n件物品,告诉你最初第i件物品的数量Ai,要求最后数量变成Bi

然后有m种交换方式,每种交换方式有两个数字a b,表示物品a能和b等价交换,是双向的

求最少交换多少次,就能实现

这题的正解是套用最小费用最大流模板。。

对着模板改了好久,终于有点像我的模板的风格了233333

解释一下模板的用法。一般把超级源点设置成s,把超级汇点设置成t

然后对于这题,一开始的时候,要让源点的一些值能够到达Ai中,所以对于每个物品,建立一条s,i的双向边容量为Ai,费用为0

因为最后的Bi全部要汇入到超级汇点中,建立一条 i,t的双向边容量为Bi,费用为0

对于m种交换,假如输入是u v,连接一条边从u,v的双向边容量为INF费用为1

建好图了,,模板里一丢,,就做完了。。

首先统计Ai的所有之和,以及Bi的所有之和,如果两者不想等,说明前后数量都不同,是不可能可以做到的,直接剪枝

记得验证最大流是否等于Bi的所有的数之和,如果不等于,说明并不是所有的都能汇入汇点中,说明这个状态是达不到的

否则,就输出最小费用。

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 400 + 5;
const int MM = 400 + 5;
const int INF = 0x3f3f3f3f;

struct Edge {
    int to, next, cap, flow, cost;
    Edge() {}
    Edge(int _to, int _next, int _cap, int _flow, int _cost) {
        to = _to; next = _next; cap = _cap; flow = _flow; cost = _cost;
    }
} E[MM];

int Head[MX], tol;
int pre[MX]; //储存前驱顶点
int dis[MX]; //储存到源点s的距离
bool vis[MX];
int N;//节点总个数,节点编号从0~N-1

void init(int n) {
    tol = 0;
    N = 2 * n + 2;
    memset(Head, -1, sizeof(Head));
}
void addedge(int u, int v, int cap, int cost) {
    E[tol] = Edge(v, Head[u], cap, 0, cost);
    Head[u] = tol++;

    E[tol] = Edge(u, Head[v], 0, 0, -cost);
    Head[v] = tol++;
}
bool spfa(int s, int t) {
    queue<int>q;
    for (int i = 0; i < N; i++) {
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = Head[u]; i != -1; i = E[i].next) {
            int v = E[i].to;
            if (E[i].cap > E[i].flow && dis[v] > dis[u] + E[i].cost) {
                dis[v] = dis[u] + E[i].cost;
                pre[v] = i;
                if (!vis[v]) {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if (pre[t] == -1) return false;
    else return true;
}

//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost) {
    int flow = 0;
    cost = 0;
    while (spfa(s, t)) {
        int Min = INF;
        for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) {
            if (Min > E[i].cap - E[i].flow)
                Min = E[i].cap - E[i].flow;
        }
        for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) {
            E[i].flow += Min;
            E[i ^ 1].flow -= Min;
            cost += E[i].cost * Min;
        }
        flow += Min;
    }
    return flow;
}

int main() {
    int n, m, s, t;
    //freopen("input.txt", "r", stdin);
    while(~scanf("%d%d", &n, &m)) {
        init(n);
        s = 0; t = 2 * n + 1;

        int s1 = 0, s2 = 0;
        for(int i = 1; i <= n; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            s1 += a; s2 += b;
            addedge(s, i, a, 0);
            addedge(i, t, b, 0);
        }
        for(int i = 1; i <= m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            addedge(u, v, INF, 1);
            addedge(v, u, INF, 1);
        }

        if(s1 != s2) {
            printf("-1\n");
            continue;
        }

        int ans = 0;
        if(minCostMaxflow(s, t, ans) == s2) {
            printf("%d\n", ans);
        } else {
            printf("-1\n");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 06:39:19

费用流 zoj3885 The Exchange of Items的相关文章

ZOJ 3885--The Exchange of Items【最小费用最大流 &amp;&amp; 建图】

The Exchange of Items Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So,

ZOJ 3885 The Exchange of Items(费用流)

ZOJ 3885 The Exchange of Items Description Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So, Bob has decided to do some b

zoj 3885 The Exchange of Items 【最小费用最大流】

The Exchange of Items Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So,

The Exchange of Items (141 - ZOJ Monthly, July 2015 - E 最小费用最大流)

The Exchange of Items Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So,

【网络流24题】No.19 负载平衡问题 (费用流)

[题意] G 公司有 n 个沿铁路运输线环形排列的仓库, 每个仓库存储的货物数量不等. 如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入文件示例input.txt517 9 14 16 4 输出文件示例output.txt11 [分析] 其实我觉得这题可以贪心啊..n^2贪心??.没细想.. 打的是费用流.. 大概这样建图: 懒得写了..凌乱之美.. 求满流费用.. 1 #include<cstdio> 2 #include<cstdlib&

POJ 3422 kaka&#39;s matrix trvals(费用流)

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <cma

hdu 2448 Mining Station on the Sea【网络费用流】

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2371    Accepted Submission(s): 732 Problem Description The ocean is a treasure house of resources and the development

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in