Bzoj 1449 [JSOI2009] team benefits (cost split, minimum cost flow)

Source: Internet
Author: User

1449: [JSOI2009] Team earnings time limit:5 Sec Memory limit:64 MB
submit:547 solved:302
[Submit] [Status] [Discuss] Descriptioninputoutput An integer that represents the minimum value of the sum of all the teams in the league. Sample Input3 3
1 0 2 1
1 1 10 1
0 1 3 3
0 S
2 3
3 1
Sample Output43
HINT


Source

Ideas

Cost split, minimum cost maximum flow.

By S to each match the Edge (1,0), which is connected by each match to both teams (1,0).

Consider winning a win W Field defeated L field, then increase the cost = ci*w^2+di*l^2-ci* (w+1) ^2-di* (l+1) ^2=2*ci*w+ci+di+2*di*l revenue. When W increases, the cost is monotonically increasing, so you can connect one (1,cost) side at a time and then w++,l--again until the game is all won, assuming that the remaining races are all lost at the beginning.

Code

1#include <cstdio>2#include <cstring>3#include <queue>4#include <vector>5 #definefor (A,B,C) for (int a= (b);a< (c); a++)6 using namespacestd;7 8typedefLong LongLL;9 Const intMAXN =8000+Ten;Ten Const intINF =1e9; One  A structedge{intU,v,cap,flow,cost; - }; - structzkw { the     intn,m,s,t; -     intVIS[MAXN],D[MAXN]; -vector<int>G[MAXN]; -Vector<edge>es; +      -     voidInitintN) { +          This->n=N; A es.clear (); at          for(intI=0; i<n;i++) g[i].clear (); -     } -     voidAddedge (intUintVintCapintCost ) { -Es.push_back (Edge) {u,v,cap,0, cost}); -Es.push_back (Edge) {v,u,0,0,-Cost }); -m=es.size (); inG[u].push_back (M-2); -G[v].push_back (M-1); to     } +     BOOLSPFA () { -memset (Vis,0,sizeof(Vis)); the          for(intI=0; i<n;i++) d[i]=INF; *queue<int>Q; $d[t]=0, vis[t]=1, Q.push (t);Panax Notoginseng          while(!Q.empty ()) { -             intU=q.front (); Q.pop (), vis[u]=0; the              for(intI=0; I<g[u].size (); i++) { +edge& e=Es[g[u][i]]; A                 intv=e.v; the                 if(es[g[u][i]^1].cap && d[v]>d[u]-e.cost) { +d[v]=d[u]-E.cost; -                     if(!Vis[v]) { $vis[v]=1; $ Q.push (v); -                     } -                 } the             } -         }Wuyi         returnd[s]!=INF; the     } -     intDfsintUinta,ll&Cost ) { Wuvis[u]=1;if(u==t)returnA; -         intUsed=0, W; About          for(intI=0; I<g[u].size (); i++) { $edge& e=Es[g[u][i]]; -             intv=e.v; -             if(D[u]-e.cost==d[v] &&!vis[v] &&e.cap) { -W=dfs (V,min (A-used,e.cap), cost); Acost+=w*E.cost; +E.cap-=w, es[g[u][i]^1].cap+=W; theUsed+=w;if(used==a)returnA; -             } $         } the         returnused; the     } the     intMincost (intSintt,ll&Cost ) { the          This->s=s, This->t=T; -         intflow=0; cost=0; in          while(SPFA ()) { thevis[t]=1; the              while(Vis[t]) { Aboutmemset (Vis,0,sizeof(Vis)); theflow+=DFS (s,inf,cost); the             } the         } +         returnflow; -     } the } MC;Bayi  the  the intn,m; - intC[MAXN],D[MAXN],WIN[MAXN],LOSE[MAXN],inch[MAXN]; -  the intMain () { thescanf"%d%d",&n,&m); theMc.init (n+m+2); the     ints=n+m,t=s+1; -for (I,0, N) thescanf"%d%d%d%d",&win[i],&lose[i],&c[i],&d[i]); the     intu,v; thefor (I,0, M) {94scanf"%d%d",&u,&v); theU--, v--;  the         inch[U]++,inch[v]++; theMc. Addedge (S,i,1,0);98Mc. Addedge (I,m+u,1,0); AboutMc. Addedge (I,m+v,1,0); -     }101LL ans=0;102for (I,0, N) {103lose[i]+=inch[i];104ans+=c[i]*win[i]*win[i]+d[i]*lose[i]*Lose[i]; the     }106for (I,0, N) {107For (J,0,inch[i]) {108Mc. Addedge (M+i,t,1,2*c[i]*win[i]+c[i]+d[i]-2*d[i]*lose[i]);109Lose[i]--, win[i]++; the         }111     } the LL cost;113 MC. Mincost (s,t,cost); theprintf"%lld", cost+ans); the     return 0; the}

Bzoj 1449 [JSOI2009] team benefits (cost split, minimum cost flow)

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.