本来是一道正常的费用流
可是我调了一早上
为什么呢
spfa里面有这么一句话
#define inc(i) i++;if(i==1005)i=1; bool spfa(){ int head=0,tail=1; memset(dis,63,sizeof dis);dis[0]=0; while(head!=tail){ inc(head);int v=dl[head];exsit[v]=0; for(int u=first[v];u;u=edge[u].next){ if(edge[u].flow&&dis[edge[u].to]>dis[v]+edge[u].cost){ dis[edge[u].to]=dis[v]+edge[u].cost; pr[edge[u].to]=u; if(!exsit[edge[u].to]){ exsit[edge[u].to]=1; inc(tail);dl[tail]=edge[u].to; } } } } return dis[T]<=inf; }
以后记住啊记住
完整代码
//Copyright(c)2016 liuchenrui #include<bits/stdc++.h> #define T 1001 #define rep(i,n) for(int i=1;i<=n;i++) #define inc(i) i++;if(i==1005)i=1; #define inf 1000000000 using namespace std; inline void splay(int &v){ v=0;char c=0;int p=1; while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();} while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();} v*=p; } int n,m,t[66][66]; struct Edge{ int to,next,flow,cost; }edge[200010]; int dis[1010],pr[1010],exsit[1010]; int first[1010],size,dl[1010]; void addedge(int x,int y,int z,int w){ size++; edge[size].to=y; edge[size].next=first[x]; first[x]=size; edge[size].flow=z; edge[size].cost=w; } void add(int x,int y,int z,int w){ addedge(x,y,z,w);addedge(y,x,0,-w); } bool spfa(){ int head=0,tail=1; memset(dis,63,sizeof dis);dis[0]=0;dl[1]=0; while(head!=tail){ inc(head);int v=dl[head];exsit[v]=0; for(int u=first[v];u;u=edge[u].next){ if(edge[u].flow&&dis[edge[u].to]>dis[v]+edge[u].cost){ dis[edge[u].to]=dis[v]+edge[u].cost; pr[edge[u].to]=u; if(!exsit[edge[u].to]){ exsit[edge[u].to]=1; inc(tail);dl[tail]=edge[u].to; } } } } return dis[T]<=inf; } int ans; void end(){ int now=T,ret=inf; while(now){ ret=min(ret,edge[pr[now]].flow); now=edge[((pr[now]-1)^1)+1].to; } now=T; while(now){ ans+=ret*edge[pr[now]].cost; edge[pr[now]].flow-=ret; edge[((pr[now]-1)^1)+1].flow+=ret; now=edge[((pr[now]-1)^1)+1].to; } } int main(){ freopen("xxx.in","r",stdin); freopen("xxx.out","w",stdout); splay(n),splay(m); rep(i,m)rep(j,n)splay(t[i][j]); rep(i,n*m)add(0,i,1,0); for(int i=n*m+1;i<=n*m+m;i++)add(i,T,1,0); rep(i,n)rep(j,m)rep(k,m)add((i-1)*m+j,n*m+k,1,t[k][i]*j); while(spfa()) end(); printf("%.2f",(double)ans/m); }