国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Poj 2195 Going Home(費用流)

2019-11-08 02:13:34
字體:
來源:轉載
供稿:網友

Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22327 Accepted: 11280 Description 這里寫圖片描述 On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a ‘.’ means an empty space, an ‘H’ rePResents a house on that point, and am ‘m’ indicates there is a little man on that point. You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house. Input There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of ‘H’s and ‘m’s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M. Output For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay. Sample Input 2 2 .m H. 5 5 HH..m ….. ….. ….. mm..H 7 8 …H…. …H…. …H…. mmmHmmmm …H…. …H…. …H…. 0 0 Sample Output 2 10 28 Source Pacific Northwest 2004

/*費用流裸題.二分圖搞.從源點向man連邊流量為1費用為0.從man向house連邊流量為1費用為曼哈頓距離.從house向匯點連邊流量為1費用為0. */#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#define MAXN 101#define INF 1e9using namespace std;int n,m,tot1,tot2,cut=1,S,T,ans;int g[MAXN][MAXN],vis[MAXN*MAXN],fa[MAXN*MAXN],head[MAXN*MAXN],dis[MAXN*MAXN];struct data{int x,y;}a[MAXN*MAXN],b[MAXN*MAXN];struct edge{int u,v,c,f,next;}e[MAXN*MAXN*2];queue<int>q;void add(int u,int v,int c,int f){ e[++cut].u=u;e[cut].v=v;e[cut].next=head[u];e[cut].c=c;e[cut].f=f;head[u]=cut; e[++cut].u=v;e[cut].v=u;e[cut].next=head[v];e[cut].c=0;e[cut].f=-f;head[v]=cut;}bool bfs(int t){ for(int i=1;i<=T;i++) dis[i]=INF;dis[0]=0; q.push(S); while(!q.empty()) { int u=q.front();q.pop();vis[u]=0; for(int i=head[u];i;i=e[i].next) { int v=e[i].v; if(dis[v]>dis[u]+e[i].f&&e[i].c) { dis[v]=dis[u]+e[i].f;fa[v]=i; if(vis[v]!=t) vis[v]=t,q.push(v); } } } return dis[T]!=INF;}void mincost(){ int t=1;ans=0; while(bfs(t)) { int tmp=fa[T],x=INF; while(tmp) x=min(x,e[tmp].c),tmp=fa[e[tmp].u]; tmp=fa[T]; while(tmp) { e[tmp].c-=x; e[tmp^1].c+=x; e[tmp].f*=x; ans+=e[tmp].f; tmp=fa[e[tmp].u]; } t++; } printf("%d/n",ans);}void slove(){ for(int i=1;i<=tot1;i++) add(0,i,1,0); for(int i=1;i<=tot2;i++) add(i+tot1,tot1+tot2+1,1,0); for(int i=1;i<=tot1;i++) for(int j=1;j<=tot2;j++) { int x=fabs(a[i].x-b[j].x)+fabs(a[i].y-b[j].y); add(i,tot1+j,1,x); } mincost(); return ;}void Clear(){ tot1=tot2=0;cut=1; memset(head,0,sizeof head); memset(b,0,sizeof b); return ;}int main(){ char ch; while(scanf("%d%d",&n,&m)) { if(!n&&!m) break; Clear(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>ch; if(ch=='m') a[++tot1].x=i,a[tot1].y=j; else if(ch=='H') b[++tot2].x=i,b[tot2].y=j; } T=tot1+tot2+1;slove(); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新巴尔虎左旗| 高要市| 额敏县| 新蔡县| 潞城市| 德保县| 城步| 肃南| 若尔盖县| 波密县| 水城县| 嘉善县| 佳木斯市| 保德县| 个旧市| 兖州市| 扶余县| 澄城县| 潜江市| 棋牌| 青州市| 色达县| 镇安县| 三河市| 聊城市| 绥阳县| 六枝特区| 阿拉尔市| 丰台区| 石嘴山市| 建昌县| 宁都县| 台州市| 五原县| 高平市| 永平县| 福建省| 乌什县| 井冈山市| 湾仔区| 马边|