神奇的上下界网络流----- (总结by : becauseofyou)(三)
;
if(in[i] > 0) add_edge(ss,i,in[i],0);
if(in[i] < 0) add_edge(i,tt,-in[i],0);
}
SAP(ss,tt,tt+1);
for(i=head[ss];i!=-1;i=edge[i].next)
{
if(edge[i].c) return false;
}
for(i = 1; i <= m; i++)
{
for(j = 1; j <= n; j++)
{
int id = ID(i,j);
for(k = head[id]; k!=-1; k=edge[k].next)
{
if(edge[k].v <= 2*n*m)
{
ans[i][j] = low[i][j] + edge[k^1].c;
}
}
}
}
for(i = 1; i <= m; i++)
{
printf("%d",ans[i][1]);
for(j = 2; j <= n; j++)
{
printf(" %d",ans[i][j]);
}
puts("");
}
/* if(check()) {
puts("YES");
} else puts("NO");*/
return true;
}
int main()
{
int t;
// freopen("budget.in","r",stdin);
// freopen("budget.out","w",stdout);
scanf("%d",&t);
for(int i = 0; i < t; i++)
{
if(i) puts("");
if(!init())
{
puts("IMPOSSIBLE");
}
else
{
if(!solve()) puts("IMPOSSIBLE");
}
}
return 0;
}
zoj 那题很好想,需要两次二分,但不是太好写,附上代码吧。
[cpp]
http://acm.zju.edu.cn/onlinejudge/showProblem.do problemCode=3496
#include
#include
typedef long long lld;
const int MAX=510;
const int INF=100000000;
struct EDGE
{
int v,c,next;
}edge[1000000];
int E,head[MAX];
int gap[MAX],cur[MAX];
int pre[MAX],dis[MAX];
void add_edge(int s,int t,int c,int cc)
{
edge[E].v=t; edge[E].c=c;
edge[E].next=head[s];
head[s]=E++;
edge[E].v=s; edge[E].c=cc;
edge[E].next=head[t];
head[t]=E++;
}