输出方案dfs一下就可以了。
As all you know, DD is a hospitable person. On Saint Valentine's Day, DD is going to arrang a party for girls and boys. However, as you may don't know, DD hate the so-called "8g" relationship. So, he will not invite a boy and a girl who have 8g at the same time.
DD's friends are M boys (labeled 1..M) and N girls (labeled 1..N), and each of them has a "lovely value". Now, DD want to invite some of them, satisfying that no 8g exists among invited people, and the total lovely value of all the invited people can be maximal.
Input
Multiple test cases, each cases consists of three parts.
First part, one line, three integers M, N and S.
Second part, two lines. First line, M integers, boys' lovely values from the 1st to the Mth. Second line, N integers, girls' lovely values from the 1st to the Nth.
Third part, S lines. Each line consists of two integers X and Y, means that the boy X and the girl Y have 8g.
One blank line between test cases.
Output
For each test case, your output should include three parts.
First part, one line, three integers X, A and B. X is the maximal total lovly value. A and B are the numbers of boys and girls invited.
Second part, one line, A integers, labels of the invited boys.
Third part, one line, B integers, labels of the invited girls.
If multiple solutions exist, output any of them.
Data Restriction
0 <= M, N <= 100; 0 <= S <= M*N.
1 <= lovely value <= 1024.
Sample Input
5 5 10 1 3 5 7 9 2 4 6 8 10 1 4 5 3 2 4 3 5 1 1 2 4 1 2 3 4 1 5 5 5
Sample Output
37 1 5 4 1 2 3 4 5
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define MAXN 2000
#define INF 0x3fFFFFFF
typedef long long ll;
struct edge
{
int to,next;
ll c;
};
edge e[999999];
int que[MAXN*100];
int dis[MAXN];
int pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed;
ll maxflow;
int en;
void add(int a,int b,ll c)
{
e[en].to=b;
e[en].c=c;
e[en].next=head[a];
head[a]=en++;
e[en].to=a;
e[en].c=0;
e[en].next=head[b];
head[b]=en++;
}
bool bfs()
{
memset(dis,-1,sizeof(dis));
que[0]=st,dis[st]=1;
int t=1,f=0;
while(f<t)
{
int j=que[f++];
for(int k=head[j];k!=-1;k=e[k].next)
{
int i=e[k].to;
if(dis[i]==-1 && e[k].c)
{
que[t++]=i;
dis[i]=dis[j]+1;
if(i==ed) return true;
}
}
}
return false;
}
int update()
{
int p;
ll flow=INF;
for (int i=pre[ed];i!=-1;i=pre[i])
if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;
for (int i=pre[ed];i!=-1;i=pre[i])
e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;
maxflow+=flow;
return p;
}
void dfs()
{
memset(pre,-1,sizeof(pre));
memcpy(head2,head,sizeof(head2));
for(int i=st,j;i!=-1;)
{
int flag=false;
for(int k=head[i];k!=-1;k=e[k].next)
if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )
{
pre[j]=i;
head2[i]=k;
i=j;
flag=true;
if(i==ed)
i=update();
if(flag)
break;
}
if (!flag) dis[i]=-1,i=pre[i];
}
}
ll dinic()
{
maxflow=0;
while(bfs())
dfs();
return maxflow;
}
int boy,girl,n,m,s,sum;
bool vis[2000];
void dfs(int u)
{
vis[u]=1;
if(0<u&&u<=n) boy++;
if(u>n&&u<=m+n) girl--;
for(int i=head[u];i!=-1;i=e[i].next)
{
int nod=i;
if(e[nod].c==0) continue;
int v=e[nod].to;
if(vis[v]) continue;
dfs(v);
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&s))
{
sum=0;
memset(head,-1,sizeof(head));en=0;
st=0,ed=n+m+1;
for(int i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
add(st,i+1,tmp);
sum+=tmp;
}
for(int i=0;i<m;i++)
{
int tmp;
scanf("%d",&tmp);
add(i+n+1,ed,tmp);
sum+=tmp;
}
for(int i=0;i<s;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v+n,INF);
}
dinic();
boy=0,girl=m;
memset(vis,0,sizeof(vis));
dfs(0);
printf("%lld %d %d\n",sum-maxflow,boy,girl);
int p=n+1;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
printf("%d",i);
p=i+1;
break;
}
}
for(;p<=n;p++)
if(vis[p]) printf(" %d",p);
printf("\n");
p=n+m+1;
for(int i=n+1;i<=n+m;i++)
{
if(!vis[i])
{
printf("%d",i-n);
p=i+1;
break;
}
}
for(;p<=n+m;p++)
if(!vis[p]) printf(" %d",p-n);
printf("\n");
}
return 0;
}