1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include<cstdio> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<cstring> #define Inf 0x3f3f3f3f using namespace std; typedef pair<int,int> P; typedef long long LL; const int MAXX=205; const double eps=0.0000001;
struct rode{ int st,en,flow,cap,cost; }; vector<rode> ro; vector<int> to[MAXX]; int n,m; int cur[MAXX],d[MAXX],guanxi[MAXX][MAXX],a[MAXX],p[MAXX]; bool vis[MAXX],inq[MAXX];
inline void add(int st,int en,int cap,int cost){ rode re;int si=ro.size(); re.st=st;re.en=en;re.flow=0;re.cap=cap;re.cost=cost; ro.push_back(re);to[st].push_back(si); re.st=en;re.en=st;re.flow=0;re.cap=0;re.cost=-cost; ro.push_back(re);to[en].push_back(si+1); }
bool bfss(int st,int en,int& flow,LL& cost){ for(int i=st;i<=en;++i){ d[i]=Inf; inq[i]=0; } queue<int> qq;qq.push(st); d[st]=0;inq[st]=true;p[st]=0;a[st]=Inf; while(!qq.empty()){ int jj=qq.front();qq.pop(); inq[jj]=false; int si=to[jj].size(); for(int i=0;i<si;++i){ rode& kk=ro[to[jj][i]]; if(kk.cap>kk.flow&&d[kk.en]>d[jj]+kk.cost){ d[kk.en]=d[jj]+kk.cost; p[kk.en]=to[jj][i]; a[kk.en]=min(a[jj],kk.cap-kk.flow); if(!inq[kk.en]){ qq.push(kk.en); inq[kk.en]=true; } } } } if(d[en]==Inf) return false; cost+=(LL)d[en]*(LL)a[en]; flow+=a[en]; int jj=en; while(jj!=st){ ro[p[jj]].flow+=a[en]; ro[p[jj]^1].flow-=a[en]; jj=ro[p[jj]].st; } return true; }
int mcmf(int st,int en,LL& cost){ int flow=0;cost=0; while(bfss(st,en,flow,cost)); return flow; }
void solve_it(){ scanf("%d",&n); int n1=2*n+1; LL minans,maxans; int ff; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j){ scanf("%d",&guanxi[i][j]); add(i,n+j,1,guanxi[i][j]); } for(int i=1;i<=n;++i){ add(0,i,1,0); add(i+n,n1,1,0); } ff=mcmf(0,n1,minans); printf("%lld\n",minans); ro.clear(); for(int i=0;i<=n1;++i) to[i].clear(); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j){ add(i,n+j,1,-guanxi[i][j]); } for(int i=1;i<=n;++i){ add(0,i,1,0); add(i+n,n1,1,0); } ff=mcmf(0,n1,maxans); printf("%lld\n",-maxans); }
int main(){
solve_it(); return 0; }
|