HDU 6779 Drink (最小费用流)

Drink

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 98    Accepted Submission(s): 41


Problem Description
一共有  n 个人,我们提供给他们三种饮料——可乐,雪碧,芬达。

每个人对这三种饮料的喜欢程度有一个顺序,一个人喝到他最喜欢的饮料可以得到 3 点快乐值,第二喜欢的饮料可以得到 2 点快乐值,第三喜欢的饮料可以得到 1 点快乐值。

我们一共有 n 瓶饮料,其中 a 瓶是可乐,b 瓶是雪碧,c 瓶是芬达,每个人恰好分到一瓶饮料。请问适当分配这些饮料, n 个人的快乐值的和最大是多少?
 
Input
第一行输入一个整数  T(2T1000) 表示 T 组数据。

每组数据第一行 4 个整数,n,a,b,c(n,a,b,c0) 分别表示人数,有 a 瓶可乐,b 瓶雪碧,c 瓶芬达。保证 a+b+c=n
接下来 n 行,每行输入一个长度为 3 的字符串,第 i 行表示第 i 个人从高到低喜好的顺序。

字符串一定是 `{"012","021","102","120","201","210"}` 中的一种。字符 0 表示可乐,1 表达雪碧,2 表示芬达,按照最喜欢,第二喜欢,第三喜欢的顺序排列。例如 021 表示最喜欢可乐,第二喜欢芬达,第三喜欢雪碧。

保证 n 之和小于等于 100000。
 
Output
对于每组数据,一行一个整数表示答案。
 
Sample Input
2 3 1 1 1 012 102 201 3 1 1 1 012 012 012
 
Sample Output
9 6
 
Source
 
Recommend
heyang
 
Statistic |  Submit |  Discuss |  Note

析:一个比较简单的费用流,只需要把每个源点向每个种字符串都连接一条边,容量就是出现的次数,费用就是0,然后再每个字符串向每种饮料连接一条边,容量就是字符串出现的次数(并没有什么影响),费用就是相应的喜好值的相反数,最后需要每个饮料向汇点连接一条边,容量就饮料的数量,费用就是0,最后跑一次最小费用流,最后的相反数就是结果。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#include <unordered_map>
#define debug() puts("++++")
#define print(x) cout<<(x)<<endl
// #define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 300 + 7;
const int maxm = 2000000 + 7;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
int n, m;

inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){
  int x;  scanf("%d", &x);  return x;
}

struct Edge{
  int from, to, cap, flow, cost;
};
 
struct MinCostMaxFlow{
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  int d[maxn];
  int p[maxn];
  int a[maxn];
  bool inq[maxn];
 
  void init(int n){
    this-> n = n;
    for(int i = 0; i < n; ++i)  G[i].cl;
    edges.cl;
  }
 
  void addEdge(int from, int to, int cap, int cost){
    edges.pb((Edge){from, to, cap, 0, cost});
    edges.pb((Edge){to, from, 0, 0, -cost});
    m = edges.sz;
    G[from].pb(m - 2);
    G[to].pb(m - 1);
  }
 
  bool bellman(int &flow, int &cost){
    ms(d, INF);  d[s] = 0;  ms(inq, 0);  inq[s] = 1;
    p[s] = 0;  a[s] = INF;
    queue<int> q;  q.push(s);
    while(!q.empty()){
      int u = q.front();  q.pop();
      inq[u] = 0;
      for(int i = 0; i < G[u].sz; ++i){
        Edge &e = edges[G[u][i]];
        if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
          p[e.to] = G[u][i];
          d[e.to] = d[u] + e.cost;
          a[e.to] = min(a[u], e.cap - e.flow);
          if(!inq[e.to]){ q.push(e.to);  inq[e.to] = 1; }
        }
      }
    }
    if(d[t] == INF)  return false;
    flow += a[t];
    cost += a[t] * d[t];
    int u = t;
    while(u != s){
      edges[p[u]].flow += a[t];
      edges[p[u]^1].flow -= a[t];
      u = edges[p[u]].from;
    }
    return true;
  }
 
  int minCostMaxFlow(int s, int t){
    this-> s = s;
    this-> t = t;
    int cost = 0, flow = 0;
    while(bellman(flow, cost));
    return cost;
  }
};
MinCostMaxFlow mcmf;

unordered_map<int, int> mp;

int main(){
  int T;  scanf("%d", &T);
  while(T--){
    scanf("%d", &n);
    int a, b, c;  mp.cl;
    mcmf.init(300);
    scanf("%d %d %d", &a, &b, &c);
    for(int i = 0; i < n; ++i){
      scanf("%d", &m);  ++mp[m];
    }
    int s = 5, t = 280;
    for(auto &it : mp){
      mcmf.addEdge(s, it.fi, it.se, 0);
      int x = it.fi;
      for(int i = 1; i <= 3; ++i, x /= 10){
        mcmf.addEdge(it.fi, x % 10, it.se, -i);
      }
    }
    mcmf.addEdge(0, t, a, 0);
    mcmf.addEdge(1, t, b, 0);
    mcmf.addEdge(2, t, c, 0);
    printf("%d\n", -mcmf.minCostMaxFlow(s, t));
  }
  return 0;
}

  

  

猜你喜欢

转载自www.cnblogs.com/dwtfukgv/p/13386731.html