"BZOJ1520" [poi2006]szk-schools km algorithm

Source: Internet
Author: User

"BZOJ1520" [poi2006]szk-schoolsdescriptioninputoutput if there is a feasible solution, the output is the least cost, otherwise the output nie.sample Input5
1 1 2 3
1 1 5 1
3 2 5 5
4 1 5 10
3 3 3 1
Sample Output9

Solving the problem: The minimum weight matching bare topic, can be directly without brain cost flow, but here is a review of the KM algorithm.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;int N,ans;int xa[210],ya[210],xb[210],yb[210],va[210],vb[210],from[210],la[210],lb[210],temp,a[210],b[210], Dis[210][210];int dfs (int x) {va[x]=1;for (int i=a[x];i<=b[x];i++) {if (!vb[i]&&! ( La[x]+lb[i]-dis[x][i]) {vb[i]=1;if (!from[i]| | DFS (From[i])) {From[i]=x;return 1;}}} return 0;} inline Int rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+gc-' 0 ', Gc=getchar (); return ret*f;} int main () {scanf ("%d", &n), int i,j,k,a,b;memset (la,0x80,sizeof (LA)), for (i=1;i<=n;i++) {a=rd (), A[i]=rd (), b[i ]=rd (), B=rd (); for (j=a[i];j<=b[i];j++) dis[i][j]=-abs (j-a) *b,la[i]=max (La[i],dis[i][j]);} for (i=1;i<=n;i++) {while (1) {memset (va,0,sizeof (VA)); memset (Vb,0,sizeof (VB)); if (Dfs (i)) break;temp=1<<30 (j=1;j<=n;j++) if (Va[j]) for (k=a[j];k<=b[j];k++) if (!vb[k]) temp=min (Temp,la[j]+lb[k]-dis[j] [K]); if (temp==1<<30) {printf ("NIE"); return 0;} for (j=1;j<=n;j++) if (Va[j]) la[j]-=temp;for (j=1;j<=n;j++) if (vb[j]) lb[j]+=temp;}} for (i=1;i<=n;i++) ans+=la[i]+lb[i];p rintf ("%d",-ans); return 0;}

"BZOJ1520" [poi2006]szk-schools km algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.