poj 2195 最小费用最大流
来源:互联网 发布:知彼手机定位wo手机 编辑:程序博客网 时间:2024/05/21 17:48
///题意:n个人和m个房子,每个房子中要安排一个人,每个人移动一步费用为1, ///所有人安排房子后需要的金钱的最小值。///曼哈顿距离:例如在平面上,坐标(x1, y1)的i点与坐标(x2, y2)的j点的曼哈顿距离为: /// d(i,j)=|X1-X2|+|Y1-Y2|.///建图:网络流,建一个超源 0,超汇 E /// 1> 超源 0 到 所有人建一条边 ,容量为 1 费用为 0 /// 2> 所有人到所有房子 建一条边 容量为1,正向费用为用: abs(x人-x房子)+abs(y人-y房子)。///反向为: -正向 /// 3> 所有房子 到 超汇(E) 建一条边 , E = 所有人个数 + 所有边个数 + 1 。容量为 1 ,费用为 0#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <vector>#include <queue>#define Max 210using namespace std;struct loc{ int x,y;}coordinate[Max];struct Edge{ int to,cap,cost,rev; ///终点,容量,费用,反向边};vector<Edge> g[Max]; ///图的邻接表queue<int> Q;char map[Max][Max];int dist[Max]; ///记录最短路径int prevv[Max]; ///最短路的前驱点int preve[Max]; ///前驱点的后继点bool vis[Max];int n,m;int sh,sm;int E;///向图中增加一条从from到to容量为cap费用为cost的边void add_edge(int from,int to,int cap,int cost){ g[from].push_back((Edge){to,cap,cost,g[to].size()}); g[to].push_back((Edge){from,0,-cost,g[from].size()-1});}int min_cost_flow(int f)///spfa求最短路,并记录路径{int res=0; while(f>0) { memset(dist,0x3f,sizeof(dist)); memset(vis,0,sizeof(vis)); dist[0]=0; vis[0]=true; Q.push(0); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=false; for(int v=0;v<g[u].size();v++) { Edge &e=g[u][v]; if(e.cap>0&&dist[e.to]>dist[u]+e.cost) { dist[e.to]=dist[u]+e.cost; prevv[e.to]=u; preve[e.to]=v; if(!vis[e.to]) { Q.push(e.to); vis[e.to]=1; } } } }f-=1; res+=dist[E]; for(int v=E;v!=0;v=prevv[v]) ///更改路径流量 { Edge &e = g[prevv[v]][preve[v]]; e.cap -= 1;g[v][e.rev].cap += 1; ///g[e.to][e.rev].cap += 1; } } return res;}int main(){while(scanf("%d%d",&n,&m) && (n+m)){int i,j,k;getchar();sh = 0,sm = 0;for(i=0;i<Max;i++){g[i].clear();}for(i=0;i<n;i++){gets(map[i]);for(j=0;j<m;j++){if(map[i][j] == 'm')/// 找到房子 计数{sm ++ ;coordinate[sm].x = i;coordinate[sm].y = j;add_edge(0,sm,1,0);///超级源点 连接 每个人}}}for(i=0;i<n;i++){for(j=0;j<m;j++){if(map[i][j] == 'H'){sh ++ ;for(k=1;k<=sh;k++){add_edge(k,sh+sm,1,abs(coordinate[k].x-i)+abs(coordinate[k].y-j));///每一个人连接 每一个房子}}}}E = sh+sm+1;for(i=sh+1;i<E;i++){add_edge(i,E,1,0);///每一个房子连接 超级汇点}int f = min(sh,sm);printf("%d\n",min_cost_flow(f));}return 0;}/*2 2.mH.5 5HH..m...............mm..H7 8...H.......H.......H...mmmHmmmm...H.......H.......H....0 0*/
0 0
- 【最小费用最大流】POJ 2195
- poj 2195 最小费用最大流
- poj 2195 (最小费用最大流)
- poj 2195 最小费用最大流
- poj 2195 最小费用最大流
- poj 2195 最小费用最大流
- poj 2195 最小费用最大流
- POJ-2195-最小费用最大流
- POJ-2195 最小费用最大流模板题
- POJ 2195(多源多汇最小费用最大流)
- poj-2195-Going Home最小费用最大流
- poj - 2195 - Going Home(最小费用最大流)
- poj 2195 最小费用最大流 EK+SPFA
- hdu 1533 || poj 2195 Going Home (最小费用最大流)
- POJ 2195 Going Home 最小费用最大流
- POJ 2195 Going Home 最小费用最大流
- poj 2195 Going Home(最小费用最大流)
- poj 2195 最小费用最大流(小人回家)
- C++数据储存方案
- Lantian和tianqi的移动开发之旅 - 001
- (转)多因子模型水平测试题
- R basic use —用R管理目录文件 (转自conan大神,纯手打,帮助记忆,可作为索引)
- java String类的使用
- poj 2195 最小费用最大流
- android 验证码之短信验证(云之讯短信平台)
- 学习目录
- 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
- 第十一次总结
- Struts 2之动态方法调用(constant)
- 【openjudge】派 二分查找
- 导入的Eclipse项目到AndroidStudio中
- ServletContext作用功能详解