POJ-2195 Going Home(最小费用最大流)

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <queue>
#define maxn 10005
#define INF 1000000000
using namespace std;
typedef long long ll;
struct Node{
Node(int a, int b){
x = a;
y = b;
}
int x, y;
};
struct Edge{
Edge(){
}
Edge(int a, int b, int c, int d, int e){
from = a;
to = b;
cap = c;
flow = d;
cost = e;
}
int from, to, cap, flow, cost;
};
vector<Edge> edge;
vector<int> v[maxn];
vector<Node> p1, p2;
int n, m;
int a[maxn], p[maxn], d[maxn], in[maxn];
char vis[maxn][maxn];
void Addedge(int from, int to, int cap, int flow, int cost){

edge.push_back(Edge(from, to, cap, 0, cost));
edge.push_back(Edge(to, from, 0, 0, -cost));
int dd = edge.size();
v[from].push_back(dd-2);
v[to].push_back(dd-1);

}
void Init(){

scanf("%d%d", &n, &m);
edge.clear();
for(int i = 0; i < maxn; i++)
v[i].clear();
p1.clear();
p2.clear();
for(int i = 0; i < n; i++){
scanf("%s", vis[i]);
for(int j = 0; vis[i][j]; j++){
if(vis[i][j] == 'H')
p2.push_back(Node(i, j));
else if(vis[i][j] == 'm')
p1.push_back(Node(i, j));
}
}
int k1 = p1.size(), k2 = p2.size();
for(int i = 0; i < k1; i++)
for(int j = 0; j < k2; j++){
}
for(int i = 0; i < k1; i++)
for(int i = 0; i < k2; i++)
}
int SPFA(){

int ans = 0, e = p1.size() + p2.size() + 1;

while(1){
a[0] = INF;
for(int i = 1; i <= e; i++)
d[i] = INF;
d[0] = 0;
memset(in, 0, sizeof(in));
queue<int> q;
q.push(0);
while(!q.empty()){

int dd = q.front();
q.pop();
in[dd] = 0;
for(int j = 0; j < v[dd].size(); j++){

Edge &e = edge[v[dd][j]];
if(d[e.to] > d[dd] + e.cost && e.cap > e.flow){
d[e.to] = d[dd] + e.cost;
a[e.to] = min(a[dd], e.cap - e.flow);
p[e.to] = v[dd][j];
if(in[e.to] == 0){
q.push(e.to);
in[e.to] = 1;
}
}
}
}
if(d[e] == INF)
break;
ans += a[e] * d[e];
for(int i = e; i != 0; i = edge[p[i]].from){
edge[p[i]].flow += a[e];
edge[p[i]^1].flow -= a[e];
}
}
return ans;
}
int main(){

//  freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &m) == 2){
if(n == 0 && m == 0)
break;
Init();
cout << SPFA() << endl;
}
return 0;
}

【最小费用最大流】【HDU1533】【Going Home】

2015-04-05 19:23:58

hdoj 1533 Going Home 【最小费用最大流】【KM入门题】

2015-08-24 14:41:12

POJ 2195-Going Home（KM算法/最小费用最大流算法）

2016-08-30 21:06:02

POJ-2195 Going Home(最小费用最大流)

2016-10-26 13:18:57

【最小费用最大流】POJ-2195 Going Home

2014-10-24 16:14:05

HDU 1533 Going Home(最小费用流)

2015-06-23 22:33:02

c语言最大流最小费用

2009年11月01日 2KB 下载

学习笔记 --- 最小费用最大流

2016-01-17 18:51:57

最小费用最大流（讲解+模板）

2016-08-07 16:40:02

最小费用最大流matlab代码

2014年09月25日 37KB 下载