• poj 2195 最小费用最大流


    #include "stdio.h"   //poj 2195 最小费用最大流
    #include "string.h"
    #include "queue"
    #include "math.h"
    using namespace std;
    
    #define N 210
    #define INF 0x3fffffff
    
    struct node{
    	int u,v,w,k;
    	int next;
    }edge[4*N*N];
    
    struct point{
    	int x,y;
    }people[N],house[N];
    
    bool mark[N];
    int start,end;
    int n,ans,idx;
    int dis[N],route[N],head[N];
    
    void init();
    bool SPFA();
    void EK();
    void adde(int u,int v,int w,int k);
    void addedge(int u,int v,int w,int k);
    
    int main()
    {
    	int L,D;
    	int i,j;
    	int x,y,w;
    	char map[105][105];
    	while(scanf("%d%d",&L,&D),L&&D)
    	{
    		for(i=1;i<=L;i++)
    			scanf("%s",map[i]+1);
    		x=y=0;
    		for(i=1;i<=L;i++)
    		{
    			for(j=1;j<=D;j++)
    			{
    				if(map[i][j]=='m'){	people[x].x = i;	people[x].y = j;  x++;}
    				if(map[i][j]=='H'){ house[y].x = i;		house[y].y = j;	  y++;}
    			}
    		}
    		init();
    		n=x;
    		start = 0;   //起点
    		end = n+n+1;   //终点
    		for(i=1;i<=n;i++)
    			adde(start,i,0,1); //1~n每个点代表人
    		for(i=1;i<=n;i++)
    		{
    			for(j=1;j<=n;j++)  //n+1~n+n每个点代表house
    			{
    				w = abs(people[i-1].x-house[j-1].x)+abs(people[i-1].y-house[j-1].y);
    				adde(i,j+n,w,1);
    			}
    		}
    		for(i=1;i<=n;i++)
    			adde(i+n,end,0,1);
    		while(SPFA())
    			EK();
    		printf("%d
    ",ans);
    	}
    	return 0;
    }	
    
    void init()
    {
    	ans = 0;
    	idx = 0;
    	memset(head,-1,sizeof(head));
    }
    
    void adde(int u,int v,int w,int k)
    {
    	addedge(u,v,w,k);
    	addedge(v,u,-w,0);
    }
    
    void addedge(int u,int v,int w,int k)
    {
    	edge[idx].u = u;
    	edge[idx].v = v;
    	edge[idx].w = w;
    	edge[idx].k = k;
    	edge[idx].next = head[u];
    	head[u] = idx;
    	idx++;
    }
    
    bool SPFA()
    {
    	int i;
    	int x,y;
    	memset(route,-1,sizeof(route));
    	memset(mark,false,sizeof(mark));
    	for(i=0;i<N;i++)	dis[i] = INF;
    	dis[0] = 0;
    	queue<int> q;
    	q.push(start);
    	mark[start] = true;
    	while(!q.empty())
    	{
    		x = q.front();
    		q.pop();
    		mark[x] = false;
    		for(i=head[x];i!=-1;i=edge[i].next)
    		{
    			y = edge[i].v;
    			if(edge[i].k && dis[y]>dis[x]+edge[i].w)
    			{
    				route[y] = i;
    				dis[y] = dis[x] + edge[i].w;
    				if(mark[y]==false)
    				{
    					mark[y] = true;
    					q.push(y);
    				}
    			}
    		}	
    	}
    	route[0] = -1;
    	if(route[end]==-1) return false;
    	return true;
    }
    
    void EK()
    {
    	int x,y;
    	y = route[end];
    	while(y!=-1)
    	{
    		x = y^1;
    		ans+=edge[y].w;
    		edge[y].k--;
    		edge[x].k++;
    		y = route[edge[y].u];
    	}
    }

  • 相关阅读:
    safari兼容时间格式 NAN
    高阶组件
    SqlBulkCopy与触发器,批量插入表(存在则更新,不存在则插入)
    SQL Server 触发器
    浅谈数据库中的触发器
    SQL Server 2008 表变量参数(表值参数)用法
    SQL Server 批量插入数据的两种方法
    SQL Server 2008 R2 主从数据库同步
    C#操作FTP, FTPHelper和SFTPHelper
    orcherd 汉化
  • 原文地址:https://www.cnblogs.com/ruo-yu/p/4411994.html
Copyright © 2020-2023  润新知