BZOJ 2055 80人環遊世界 有上下界的費用流,bzoj2055
題目大意:給定n個點,每個點有固定的經過次數,m個人從任意節點出發任意節點結束,只能向右走,要求總邊權和最小
有源匯、有上下界的費用流
其實上下界費用流有兩種寫法- - 一種是按照上下界網路流那麼轉化- - 一種是把必經邊的費用減掉一個INF 跑完再加回去
我比較傾向於第一種寫法- - 第二種寫法在INF的取值上有點麻煩- -
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 220#define S 210#define T 211#define _S 212#define _T 213#define INF 0x3f3f3f3fusing namespace std;struct abcd{int to,flow,cost,next;}table[1001001];int head[M],tot=1;int n,m,ans;void Add(int x,int y,int f,int c){table[++tot].to=y;table[tot].flow=f;table[tot].cost=c;table[tot].next=head[x];head[x]=tot;}void Link(int x,int y,int f,int c){Add(x,y,f,c);Add(y,x,0,-c);}bool Edmonds_Karp(){static int q[65540],flow[M],cost[M],from[M];static unsigned short r,h;static bool v[M];int i;memset(cost,0x3f,sizeof cost);flow[S]=INF;cost[S]=0;q[++r]=S;while(r!=h){int x=q[++h];v[x]=0;for(i=head[x];i;i=table[i].next)if(table[i].flow&&cost[table[i].to]>cost[x]+table[i].cost){cost[table[i].to]=cost[x]+table[i].cost;flow[table[i].to]=min(flow[x],table[i].flow);from[table[i].to]=i;if(!v[table[i].to])v[table[i].to]=true,q[++r]=table[i].to;}}if(cost[T]==INF) return false;ans+=flow[T]*cost[T];for(i=from[T];i;i=from[table[i^1].to])table[i].flow-=flow[T],table[i^1].flow+=flow[T];return true;}int main(){int i,j,x;cin>>n>>m;Link(_T,_S,m,0);for(i=1;i<=n;i++){scanf("%d",&x);Link(S,i<<1,x,0);Link(i+i-1,T,x,0);Link(_S,i+i-1,INF,0);Link(i<<1,_T,INF,0);}for(i=1;i<=n;i++)for(j=i+1;j<=n;j++){scanf("%d",&x);if(~x) Link(i<<1,j+j-1,INF,x);}while( Edmonds_Karp() );cout<<ans<<endl;return 0;}