BZOJ 1070 [SCOI2007] minimum cost flow of vehicle repair

Source: Internet
Author: User

Test Instructions:Link Method:Minimum cost maximum flow parsing:A few days ago to do the cost of flow, very classic, very magical, very 6 a problem, but did not have time to write a blog, so today sent him up. So I did not look at the problem before I did not work out this question, then full of back? Flow? Thoughts are not going to come out of anything. After reading the puzzle feel good magic! Suppose that n person m car, then each person has a line of state, corresponding to what is it? It was his last few fixed which car, and this is not sure, so we have to connect this point to the M car, the flow is 1, and the cost is the countdown to the number of times the repair time, why? If this person is the last one to fix this car, then everyone else needs to wait for him to finish, so take this. Please see the code for the two loops to draw a picture. Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 11000#define M 101000#define INF 0x3f3f3f3fusing namespace STD;int Map[ $][ $];intN,m,cnt,s,t;structnode{intFrom,to,val,cost,next;} EDGE[M];intHead[n],dis[n],v[n],f[n];voidInit () {memset(head,-1,sizeof(head)); Cnt=0;}voidEdgeadd (intFromintTo,intValintCost) {edge[cnt].to=to;    Edge[cnt].val=val;    Edge[cnt].from=from;    Edge[cnt].cost=cost;    Edge[cnt].next=head[from]; head[from]=cnt++;}intSPFA (intSintE) {memset(Dis,0x3f,sizeof(dis));memset(V,0,sizeof(v));memset(f,-1,sizeof(f)); Queue<int>Q    Q.push (s); dis[s]=0; v[s]=1; while(!q.empty ()) {intU=q.front ();        Q.pop (); v[u]=0; for(inti=head[u];i!=-1; i=edge[i].next) {intto=edge[i].to;if(edge[i].val!=0&&dis[u]+edge[i].cost<dis[to]) {dis[to]=dis[u]+edge[i].cost; F[to]=i;if(!v[to])                    {Q.push (to); v[to]=1; }            }        }    }if(dis[e]!=0x3f3f3f3f)return 1;return 0;}voidMCMF () {intans=0; while(SPFA (s,t)) {intFlow=inf; for(inti=f[t];i!=-1; I=f[edge[i].from]) flow=min (flow,edge[i].val); for(inti=f[t];i!=-1; I=f[edge[i].from]) {edge[i].val-=flow; edge[i^1].val+=flow;    } Ans+=flow*dis[t]; }printf("%.2lf\n",(Double) ans/m);}intMain () {scanf("%d%d", &n,&m); s=0, t=n*m+m+1; Init (); for(intI=1; i<=m;i++) { for(intj=1; j<=n;j++) {scanf("%d",&MapI        [j]); }    } for(intI=1; i<=n*m;i++) {Edgeadd (s,i,1,0); Edgeadd (I,s,0,0); } for(inti=n*m+1; i<=n*m+m;i++) {Edgeadd (i,t,1,0); Edgeadd (T,i,0,0); } for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) { for(intk=1; k<=m;k++) {Edgeadd ((I-1) *m+j,n*m+k,1,MapK                [I]*j]; Edgeadd (N*m+k, (i-1) *m+j,0,-MapK            [I]*j]; }}} MCMF ();}

BZOJ 1070 [SCOI2007] minimum cost flow of vehicle repair

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.