1. 程式人生 > >A Birthday 網路流最小費用最大流

A Birthday 網路流最小費用最大流

題目連結

題意:n只蠟燭,m個區域,第i個蠟燭可以放在第a_{i}或者b_{i}區域裡,每個區域所耗時間等於佔用該區域蠟燭個數的平方。求總的最小消耗時間。

思路:可以看成每個蠟燭可以流向兩個區域,建立兩個容量為1,費用為0的邊,建一個源點與n個蠟燭連邊,容量為1,費用為0,再建一個匯點讓m個區域與之連邊,容量為每個區域對應的入度x,費用為x^{2},那麼我們可以把這一條邊拆成x條邊,第i條邊容量為1,費用為2*i-1。這樣由於求最小費用,所以當流量為f時,經過的邊即為第1條到第f條邊,總費用=f*f,然後跑一遍最小費用最大流就行了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define NUM 50010
#define debug true
#define lowbit(x) ((-x)&x)
#define ffor(i,d,u) for(int i=d;i<=u;++i)
#define _ffor(i,u,d) for(int i=u;i>=d;--i)
#define mst(array,Num) memset(array,Num,sizeof(array))
const int p = 1e9+7;
int n,m,ednum;
int head[105],h[105]={};
int pre[105],dist[105];
int a[51],b[51],ss[51]={};
struct edge
{
    int next,to,f,c;
}e[8010];
struct Vertex
{
    int id,dis;
    bool operator>(const Vertex &x)const
    {
        return dis > x.dis ;
    }
};
template <typename T>
inline void read(T &x){
    char ch = getchar();x = 0;
    for (; ch < '0' || ch > '9'; ch = getchar());
    for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}
template <typename T>
inline void write(T x)
{
    int len=0;char c[21];
    if(x<0)putchar('-'),x*=(-1);
    do{++len;c[len]=(x%10)+'0';}while(x/=10);
    _ffor(i,len,1)putchar(c[i]);
}
inline void addedge(const int &x,const int &y,const int &flow,const int &cost)
{
    e[++ednum].to = y,e[ednum].f = flow,e[ednum].next = head[x],head[x] = ednum,e[ednum].c = cost;
    e[++ednum].to = x,e[ednum].f = 0,e[ednum].next = head[y],head[y] = ednum,e[ednum].c = -cost;
}
inline bool Dij(const int &s,const int &t)
{
    Vertex x,y;
    priority_queue < Vertex , vector < Vertex > , greater < Vertex > > q;
    mst(dist,INF);
    x.id = s,dist[s] = x.dis = 0;
    pre[s] = -1;
    q.push(x);
    while(!q.empty())
    {
        x = q.top(),q.pop();
        if(x.dis > dist[x.id])continue;
        for(int i = head[x.id] ; i != -1 ; i = e[i].next)
        {
            y.id = e[i].to;
            if(e[i].f > 0 && dist[y.id] > dist[x.id]+e[i].c+h[x.id]-h[y.id])
            {
                y.dis = dist[y.id] = dist[x.id]+e[i].c+h[x.id]-h[y.id];
                pre[y.id] = i;
                q.push(y);
            }
        }
    }
    if(dist[t] == INF)return false;
    return true;
}
inline void MinCost_MaxFlow(const int &s,const int &t,int &cost,int &maxflow)
{
    int d=INF;
    while(d > 0&&Dij(s,t))
    {
        ffor(i,0,t) h[i] += dist[i];
        d = INF;
        for(int i = pre[t] ; i != -1 ; i = pre[e[i^1].to])
            d = min(d , e[i].f);
        maxflow += d , cost += h[t] * d;
        for(int i = pre[t] ; i != -1 ; i = pre[e[i^1].to])
        {
            e[i].f -= d;
            e[i^1].f += d;
        }
    }
}
inline void AC()
{
    int x,y,s,t;
    int cost,maxflow=0;
    read(n),read(m);
    s=0,t=n+m+1;
    maxflow = 0,ednum = -1,mst(head,-1);
    ffor(i,1,n)
    {
        read(a[i]),read(b[i]);
        addedge(0,i,1,0);
        addedge(i,a[i]+n,1,0);
        addedge(i,b[i]+n,1,0);
        ++ss[a[i]],++ss[b[i]];
    }
    ffor(i,1,m)
    {
        cost = 1;
        ffor(j,1,ss[i])
        {
            addedge(i+n,t,1,cost);
            cost += 2;
        }
    }
    cost = 0;
    MinCost_MaxFlow(s,t,cost,maxflow);
    write(cost),puts("");
}
int main()
{
    AC();
    return 0;
}