HDU 2426 Interesting Housing Problem 最小费用最大流

这道题貌似是08年杭州网络赛的题目, 看完题后就发现是个最优匹配问题,用KM或者最小费用最大流做, 就找了个最小费用最大流的模板,改了一下,就能过了、

建图还是比较好想的,建立一个超级源点,一个超级汇点,然后源点与学生连边,流量限制为1,费用为0,汇点与房间连边,流量限制为1, 费用为0, 然后学生与房间之间的边,流量限制是1,费用就是题目给的喜好值了, 注意,每次加边,都要加一个相应的反向边, 就是流量为0, 费用为正向边的负数。

然后题目中要求不能把学生分到不喜欢的房间里,所以碰见喜好值是负数就不管了。 另外,由于是最小费用么,所以所有喜好值都是乘以-1后加进去的,这样求出的最小值,输出的时候再乘以-1就变成最大的了。

最后判断能不能所有学生都分配到房间里, 就判断流量的大小是否等于学生个数就行了


/* ID: sdj22251 PROG: subset LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 100005 #define eps 1e-11 #define L(x) x<<1 #define R(x) x<<1|1 using namespace std; int tot = 0, x, y; class mincost { private: const static int V = 2001; //注意点的个数, 应该为学生+房间+2, 所以至少要开到1002 const static int E = 2000001; const static int INF = -1u >> 1; struct Edge { int v, cap, cost; Edge *next; } pool[E], *g[V], *pp, *pree[V]; int T, S, dis[V], pre[V]; int n, m, flow, cirq[V]; void SPFA(); inline void addedge(int i, int j, int cap, int cost); public: bool initialize(int x, int y, int z); void mincost_maxflow(); }; void mincost::mincost_maxflow() { while (true) { SPFA(); if (dis[T] == INF) break; int minn = INF; for (int i = T; i != S; i = pre[i]) minn = min(minn, pree[i]->cap); for (int i = T; i != S; i = pre[i]) { pree[i]->cap -= minn; pool[(pree[i] - pool)^0x1].cap += minn; flow += minn * pree[i]->cost; } tot += minn; //流量计算 } if(tot != x) flow = 1; printf("%d\n", -flow); } void mincost::SPFA() { bool vst[V] = {false}; int tail = 0, u; fill(dis,dis + n,0x7fffffff); cirq[0] = S; vst[S] = 1; dis[S] = 0; for (int i = 0; i <= tail; i++) { int v = cirq[i % n]; for (Edge *i = g[v]; i != NULL; i = i->next) { if (!i->cap) continue; u = i->v; if (i->cost + dis[v] < dis[u]) { dis[u] = i->cost + dis[v]; pree[u] = i; pre[u] = v; if (!vst[u]) { tail++; cirq[tail % n] = u; vst[u] = true; } } } vst[v] = false; } } void mincost::addedge(int i, int j, int cap, int cost) { pp->cap = cap; pp->v = j; pp->cost = cost; pp->next = g[i]; g[i] = pp++; } bool mincost::initialize(int x, int y, int z) { memset(g, 0, sizeof (g)); pp = pool; n = x + y + 2; //n即为顶点的个数 学生数+房间数+一个源点+一个汇点 m = z; S = 0; T = x + y + 1; int v, u, f, c; for (int i = 0; i < m; i++) { scanf("%d %d %d", &v, &u, &c); v++; u++; if(c >= 0) { addedge(v, u + x, 1, -c); addedge(u + x, v, 0, c); } } for(int i = 1; i <= x; i++) { addedge(0, i, 1, 0); addedge(i, 0, 0, 0); } for(int i = 1; i <= y; i++) { addedge(x + i, T, 1, 0); addedge(T, x + i, 0, 0); } flow = 0; return true; } mincost g; int main() { //freopen("d:/data.in","r",stdin); //freopen("d:/data.out","w",stdout); int z, cas = 0; while(scanf("%d%d%d", &x, &y, &z) != EOF) { g.initialize(x, y, z); printf("Case %d: ", ++cas); tot = 0; g.mincost_maxflow(); } return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值