40th Asia Region, Tehran Site, Problem I: Cafebazaar【最大費用可行流】

題目大意

就是有n個人,m個app,有幾個人是關鍵人,必須得開發app,有幾個app是關鍵app,必須得被開發。
然後每個人僅可以開發指定的幾個app,分別可獲得不同的錢
每個人僅可開發一個app,一個app僅可有一個人開發。
問你,在關鍵人和關鍵app能不能滿足,能的話,最大錢是多少。

思路

以樣例爲例:

2 4
1 1
1 3
2 1 8 2 10
3 2 2 3 10 4 50

建立網絡流如上,所有邊的容量都爲1,關鍵點的權值置爲INF,中間的邊權值爲開發app的錢,然後跑一次最大費用可行流。

最大費用可行流,在最小費用最大流的模板上稍加修改就能實現。

先說最小費用流

算法步驟
初始化伴隨網絡:<v,u>.cost=-<u,v>.cost,<v,u>.cap=flow=0
即對原網絡中每一條邊,添加一條容量爲0的反向邊,費用取相反數,表示這正向邊少流可以減少費用。

然後每次在伴隨網絡裏用SPFA求費用最短的增廣路

一開始 反向負費用的邊 ,容量都是0
所以第一次跑出來的最短路,費用一定是正的
然後沿着這條增廣路增廣一次, 增廣途中就有反向邊的容量被增加了
然後第二次跑最短路,就有可能跑出負費用
如果是負費用,說明費用還可以優化
但如果再次跑出了正費用,說明費用已經沒法優化了,但流還可以繼續增廣
這時候繼續進行到底,就是最小費用最大流;停住,就是最小費用可行流

最大費用只需把邊的費用取相反數,最後答案也取相反數即可。

代碼有點醜將就着看看吧- -。

AC代碼

/*
40th 2015-2016 Asia Region, Tehran Site
Problem I: Cafebazaar
*/ 

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <list>
//#include <unordered_map>
using namespace std;
#define CLR(x,y) memset((x),(y),sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 lll;

const int MAXN = 10000;
const int MAXM = 100000;
const ll INF = 0x3f3f3f3f;
struct Edge
{
    int to, next, cap, flow, cost;
}edge[MAXM];
int head[MAXN], tol;
int pre[MAXN];
ll dis[MAXN];
bool vis[MAXN];
int N;//節點總個數
void init(int n)
{
    N = n;
    tol = 0;
    CLR(head,-1);
}
void addedge(int u, int v, int cap, int cost)
{
    edge[tol].to = v;
    edge[tol].cap = cap;
    edge[tol].cost = -cost;//最大費用 
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    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 = edge[i].next)
        {
            int v = edge[i].to;
            if (edge[i].cap > edge[i].flow &&
                dis[v] > dis[u] + edge[i].cost)
            {
                dis[v] = dis[u] + edge[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 maxCostflow(int s, int t, ll &cost)
{
    int flow = 0;
    cost = 0;
    bool first=0;
    while (spfa(s, t))
    {
        int Min = INF;
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
        {
            if (Min > edge[i].cap - edge[i].flow)
                Min = edge[i].cap - edge[i].flow;
        }
        ll c=0;
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
        {
            edge[i].flow += Min;
            edge[i ^ 1].flow -= Min;
            c += edge[i].cost * Min;
        }
        //printf("cost:%d\n",c);
        if(first)first=0;
        else if(c>=0)return flow;
        cost+=c;
        flow += Min;
    }
    return flow;
}

bool has_id[300];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m),n)
    {
        CLR(has_id,0);
        init(n+m+1);
        int t,cntINF=0;
        scanf("%d",&t);
        cntINF+=t;
        while(t--)
        {
            int x;
            scanf("%d",&x);
            addedge(0,x,1,INF);
            has_id[x]=1;
        }
        for(int i=1 ; i<=n ; ++i)
        {
            if(!has_id[i]) addedge(0,i,1,0);
        }
        scanf("%d",&t);
        cntINF+=t;
        while(t--)
        {
            int x;
            scanf("%d",&x);
            x+=n;
            addedge(x,n+m+1,1,INF);
            has_id[x]=1;
        }
        for(int i=n+1 ; i<=n+m ; ++i)
        {
            if(!has_id[i])addedge(i,n+m+1,1,0);
        }
        for(int i=1 ; i<=n ; ++i)
        {
            int t;
            scanf("%d",&t);
            while(t--)
            {
                int v,w;
                scanf("%d%d",&v,&w);
                v+=n;
                addedge(i,v,1,w);
            }
        }
        ll ans=0;
        maxCostflow(0,n+m+1,ans);
        ans=-ans;
        ans-=cntINF*INF;
        if(ans>=0)printf("%lld\n",ans);
        else printf("-1\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章