zoj 3165
最大权独立子集
检查了四个小时,最后发现是输出的问题,哎,抓狂
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=200*2;
const int maxe=maxn*maxn*4;
const int inf=1<<28;
int val[maxn];
int m,n,s,t,tot,clo;
struct Edge
{
int from,to,next;
};
Edge edges[maxe];
int p[maxn],num[maxn],flow[maxe],cap[maxe],d[maxn],cur[maxn],head[maxn],x[maxn],y[maxn];
void addedge(int from,int to,int ecap)
{
edges[tot].from=from;
edges[tot].to=to;
cap[tot]=ecap;
edges[tot].next=head[from];
head[from]=tot;
tot++;
edges[tot].from=to;
edges[tot].to=from;
cap[tot]=0;
edges[tot].next=head[to];
head[to]=tot;
tot++;
}
bool bfs()
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i!=-1;i=edges[i].next)
{
Edge& e=edges[i];
if(d[e.to]==-1&&cap[i]>flow[i])
{
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
if(d[t]!=-1) return true;
else return false;
}
int dfs(int x,int a)
{
if(x==t||a==0) return a;
int tflow=0,f;
for(int i=cur[x];i!=-1;i=edges[i].next)
{
Edge& e=edges[i];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,cap[i]-flow[i])))>0)
{
flow[i]+=f;
flow[i^1]-=f;
tflow+=f;
a-=f;
cur[x]=i;
if(a==0) break;
}
}
return tflow;
}
int maxflow()
{
int tflow=0;
while(bfs())
{
memcpy(cur,head,(t+1)*sizeof(int));
tflow+=dfs(s,inf);
}
return tflow;
}
void init()
{
tot=0;
memset(flow,0,sizeof(flow));
memset(cap,0,sizeof(cap));
memset(head,-1,sizeof(head));
}
int main()
{
while(cin>>m>>n>>clo)
{
init();
s=0;t=m+n+1;
int i,j,amount=0;
for(i=1;i<=m;i++)
{
scanf("%d",&val[i]);
amount+=val[i];
addedge(s,i,val[i]);
}
for(j=1;j<=n;j++)
{
scanf("%d",&val[m+j]);
amount+=val[m+j];
addedge(m+j,t,val[m+j]);
}
int u,v;
for(i=1;i<=clo;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v+m,inf);
}
int ou=maxflow();
int totx=0,toty=0;
for(i=1;i<=m;i++) if(d[i]!=-1) x[totx++]=i;
for(j=1;j<=n;j++) if(d[m+j]==-1) y[toty++]=j;
printf("%d %d %d\n",amount-ou,totx,toty);
for(i=0;i<totx-1;i++) printf("%d ",x[i]);
if(totx) printf("%d",x[totx-1]);
printf("\n");
for(i=0;i<toty-1;i++) printf("%d ",y[i]);
if(toty) printf("%d\n",y[toty-1]);
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号