BZOJ 1070: [SCOI2007]修车(费用流)
又是一道水题= =,突然发现第一页下面好多水题,等我A了它们= =
可以发现ans=sigma(t[i]*k) k表示是第几个修,然后将m拆成m*n个点,表示第几次修,从s向n*m个点连流量为1的边,从n*m个点分别向车连权值为t[i]*k的边,然后再从车向T连边就行了(和noi的某道题一样还有GDKOI2014T2一样)
CODE:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #define maxn 650 #define maxm 180000 #define inf 0x7fffffff using namespace std; struct edges{ int to,cap,dist,next; }edge[maxm]; int s,t,next[maxn],l; int addedge( int from, int to, int cap, int dist){ l++; edge[l*2]=(edges){to,cap,dist,next[from]}; edge[l*2+1]=(edges){from,0,-dist,next[to]}; next[from]=l*2;next[to]=l*2+1; return 0; } bool b[maxn]; int dist[maxn],way[maxn]; queue< int > q; bool spfa(){ for ( int i=1;i<=t;i++) dist[i]=inf; memset (b,0, sizeof (b)); dist[s]=0; q.push(s); while (!q.empty()){ int u=q.front();q.pop(); b[u]=0; for ( int i=next[u];i;i=edge[i].next) if (edge[i].cap&&dist[u]+edge[i].dist<dist[edge[i].to]) { dist[edge[i].to]=dist[u]+edge[i].dist; way[edge[i].to]=i; if (!b[edge[i].to]){ b[edge[i].to]=1;q.push(edge[i].to); } } } if (dist[t]==inf) return 0; return 1; } int mcmf(){ int cost=0; while (spfa()){ cost+=dist[t]; int x=t; while (x!=s){ edge[way[x]].cap-=1; edge[way[x]^1].cap+=1; x=edge[way[x]^1].to; } } return cost; } int n,m; int main(){ scanf ( "%d%d" ,&m,&n); s=n*m+n+1;t=n*m+n+2; for ( int i=1;i<=n;i++) for ( int j=1;j<=m;j++){ int x; scanf ( "%d" ,&x); for ( int k=1;k<=n;k++) addedge((k-1)*m+j,n*m+i,1,x*k); } for ( int i=1;i<=n*m;i++) addedge(s,i,1,0); for ( int i=1;i<=n;i++) addedge(n*m+i,t,1,0); printf ( "%.2lf\n" ,mcmf()*1.0/n); return 0; } |