洛谷新生舞会(错误代码
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<set> #include<map> #include<stack> #include<cstring> #define inf 2147483647 #define ls rt<<1 #define rs rt<<1|1 #define lson ls,nl,mid,l,r #define rson rs,mid+1,nr,l,r #define N 1100 #define For(i,a,b) for(long long i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() using namespace std; long long n,s,t,tot; long long a[N][N],b[N][N],d[N]; double l,r,mid,maxflow,mincost,eps=1e-8; bool vis[N]; long long last[N],flow[N],pre[N],head[N]; struct node{ long long n; long long v; double w; long long next; }e[N]; queue<long long>q; void in(long long &x){ long long y=1; char c=g();x=0; while(c<'0'||c>'9'){ if(c=='-')y=-1; c=g(); } while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g(); } x*=y; } void o(long long x){ if(x<0){ p('-'); x=-x; } if(x>9)o(x/10); p(x%10+'0'); } void push(long long x,long long y,long long v,double w){ e[tot].n=y; e[tot].v=v; e[tot].w=w; e[tot].next=head[x]; head[x]=tot++; } bool spfa(long long s,long long t){ //memset(d,0,sizeof(d)); memset(flow,0x7f,sizeof(flow)); memset(vis,0,sizeof(vis)); For(i,0,t) d[i]=0; q.push(s); vis[s]=1; d[s]=0; pre[t]=-1; while(!q.empty()){ long long now=q.front(); q.pop(); vis[now]=0; for(long long i=head[now];i!=-1;i=e[i].next) if(e[i].v&&d[e[i].n]-d[now]-e[i].w<eps){ d[e[i].n]d[now]+e[i].w; pre[e[i].n]=now; last[e[i].n]=i; flow[e[i].n]=min(flow[now],e[i].v); if(!vis[e[i].n]){ vis[e[i].n]=1; q.push(e[i].n); } } } return pre[t]!=-1; } bool MCMF(long long s,long long t){ while(spfa(s,t)){ long long now=t; maxflow+=flow[t]; mincost+=(double)flow[t]*d[t]; while(now!=s){ e[last[now]].v-=flow[t]; e[last[now]^1].v+=flow[t]; now=pre[now]; } } return mincost>=0; } bool check(double c){ s=0;t=2*n+1; memset(e,0,sizeof(e)); memset(head,-1,sizeof(head)); For(i,1,n){ push(s,i,1,0);push(i+n,t,1,0); } For(i,1,n) For(j,1,n) push(i,j+n,1,a[i][j]-c*b[i][j]); return MCMF(s,t); } int main(){ in(n); For(i,1,n) For(j,1,n) cin>>a[i][j]; For(i,1,n) For(j,1,n) cin>>b[i][j]; l=0.0;r=900.0; while(r-l>eps){ mid=(l+r)/2.0; if(check(mid)) l=mid; else r=mid; } printf("%.6f",l); //cout<<l; return 0; }