Going Home 最小費用流 spay

2022-05-13 18:09:23 字數 797 閱讀 2446

題意:有n個人回家,每移動一步花費1,求讓所有的人回家的最小花費

題解:建立乙個源點s與所有的人連線flow為1,cost為0,乙個匯點t與所有的房子連線flow為1,cost為0,然後吧每個人與每個房子連線,權值flow=1,花費為曼哈頓距離.然後求s到t的最小費用流

//#include#include#include#include#include #include#define min(a,b) ag[maxn];

char mp[120][120];

void add_edge(int from,int to,int flow,int cost)

int spaf()}}

}return dis[t];

}int min_cost()

// cout<<"aug="

ans+=tmp;

max_flow+=aug;

}return ans;

}int main()

edge_num=0;

s=n*m+1,t=s+1;

int h[110],c[110],cnt1=0;

int h1[110],c1[110],cnt2=0;

for(int i=1;i<=n;i++)

else if(mp[i][j]=='m')}}

for(int i=0;i}

printf("%d\n",min_cost());

for(int i=0;i<=t;i++)

}}

POJ2195 Going home 最小費用流

建立源點s,匯點t,s與每個人之間建邊,費用為0,容量為1,每個房子與t建邊,費用為0容量為1,每個人與每個房子之間建邊,費用為人到達房子的距離,容量為1,則該題轉化為求最小費用流問題。include include include include define max 1 31 1 define ...

hdu 1533 Going Home 最小費用流

建圖很簡單 bfs預處理地圖,距離就為費用 源點到所有m建邊,流量1費用0 m到所有h建邊,流量1費用為距離 h到所有匯點建邊,流量1費用0 include include include includeusing namespace std define maxn 10005 define max...

最小費用流

include include define maxn 61 define maxv maxn maxn 2 1 define maxe maxv 5 define oo 2147483647 define min a,b a b b a define maxq maxe using namespa...