Beloved Sons(zju1338,KM)

来源:互联网 发布:虎门淘宝服装摄影 编辑:程序博客网 时间:2024/05/01 23:54

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1338

ZOJ Problem Set - 2362

Beloved Sons

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Once upon a time there lived a king and he had N sons. And the king wanted to marry his beloved sons on the girls that they did love. So one day the king asked his sons to come to his room and tell him whom do they love.

But the sons of the king were all young men so they could not tell exactly whom they did love. Instead of that they just told him the names of the girls that seemed beautiful to them, but since they were all different, their choices of beautiful girls also did not match exactly.

The king was wise. He did write down the information that the children have provided him with and called you, his main wizard.

"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. So please, prepare the marriage list."

"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. So please, prepare the marriage list."

So, go on, make a list to maximize the king's happiness.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

The first line of the input file contains N - the number of king's sons (1 <= N <= 400). The second line contains N integer numbers Ai ranging from 1 to 1000 - the measures of king's love to each of his sons.

Next N lines contain lists of king's sons' preferences - first Ki - the number of the girls the i-th son of the king likes, and then Ki integer numbers - the girls he likes (all potentially beautiful girls in the kingdom were numbered from 1 to N, you know, beautiful girls were rare in those days).

Output

Output N numbers - for each son output the number of the beautiful girl he must marry or 0 if he must not marry the girl he likes.

Denote the set of sons that marry a girl they like by L, then you must maximize the value of

Sample Input

1

4

1 3 2 4

4 1 2 3 4

2 1 4

2 1 4

2 1 4

Sample Output

2 1 0 4

Author: Andrew Stankevich

Source: Andrew Stankevich's Contest #3

Submit    Status

time  memeory

310 856

解析:

     国王有N个儿子,每对每个儿子有不同的喜爱程度a[i]

     儿子i中意ki个女孩。当儿子选到中意的女孩时,国王就会很开心,开心值=sqrt((ai^2+...));

     问如何安排婚事使得国王开心

 思路:最大权匹配套用模板

1,建图 将男生编号看做X集合上的点,女生编号看做Y集合上的点,w[i][j]=a[i]*a[i]表示XY的权值 

2.利用匈牙利算法+KM,进行匹配并且记录路径

3.整理记录结果 

*/

#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>#include<iostream>const int maxn=410;int lack,n;int inf=214748364;int a[maxn],m[maxn];int w[maxn][maxn];//路径权值 int lx[maxn],ly[maxn];//分别表示x和y的顶标,并且lx应初始化为inf,ly为0 int linky[maxn];//linky[i]=j表示i与j相连 int visx[maxn],visy[maxn];bool find(int x)//寻找交错路 {visx[x]=true;for(int y=1;y<=n;y++){if(visy[y])continue;int t=lx[x]+ly[y]-w[x][y];//printf("t==%d",t);if(t==0) { visy[y]=true; if(linky[y]==-1||find(linky[y])) {  linky[y]=x;  return true; } } else if(lack>t)lack=t; //printf("lack %d",lack);}return false;}void KM(){memset(linky,-1,sizeof(linky));//memset(lx,-inf,sizeof(lx));memset(ly,0,sizeof(ly));//printf("n==%d",n);for(int i=1;i<=n;i++){lx[i]=-inf;for(int j=1;j<=n;j++)   if(lx[i]<w[i][j]){lx[i]=w[i][j]; } }// for(int i=1;i<=n;i++)//printf("lx[i]==%d\n ",lx[i]); for(int x=1;x<=n;x++) { for(;;) {  memset(visy,0,sizeof(visy));  memset(visx,0,sizeof(visx));  lack=inf;  if(find(x))  break;  for(int i=1;i<=n;i++)  {  if(visx[i])  lx[i]-=lack;  if(visy[i])     ly[i]+=lack;  } } }}void print()//处理记录结果 { memset(m,0,sizeof(m));     for(int i=1;i<=n;i++)     {       if(w[linky[i]][i])        m[linky[i]]=i;        else        m[linky[i]]=0;     }     printf("%d",m[1]);     for(int i=2;i<=n;i++)     printf(" %d",m[i]);     printf("\n");}int main(){int T,i,j;scanf("%d",&T);while(T--){scanf("%d",&n);int k,g;memset(w,0,sizeof(w));for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++)  {  scanf("%d",&k);  for(j=1;j<=k;j++)  {   scanf("%d",&g);   w[i][g]=a[i]*a[i];  }  }KM();print();  if(T)  printf("\n");//注意输出格式 }return 0;}PS :由于对这个模板不熟,坑了我一整天啊!!!!!!!!!!!!!!!!下面是L的做法:貌似简单些#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 510int uN,vN;int g[maxn][maxn];int link[maxn];bool use[maxn];struct point{int num,index;bool operator<(const point &x)const{return num>x.num;}}like[maxn];bool dfs(int u){int v;for(v=1;v<=vN;v++)if(g[u][v]&&!use[v]){use[v]=true;if(link[v]==-1 || dfs(link[v])){link[v]=u;return true;}}return false;}int hungery(){int res=0;int u;memset(link,-1,sizeof(link));for(u=1;u<=uN;u++){memset(use,0,sizeof(use));if(dfs(like[u].index)) res++;}return res;}int main(){int i,n,j,k,m;int T;scanf("%d",&T);while(T--){printf("\n");scanf("%d",&n);        for(i=1;i<=n;i++){scanf("%d",&like[i].num);like[i].index=i;}sort(like+1,like+n+1);//for(i=1;i<=n;i++)//printf("%d %d\n",like[i].num,like[i].index);memset(g,0,sizeof(g));        for(i=1;i<=n;i++){scanf("%d",&m);for(j=1;j<=m;j++){scanf("%d",&k);g[i][k]=1;}}uN=n; vN=n;int num=hungery();int a[510];memset(a,0,sizeof(a));for(i=1;i<=n;i++){  if(link[i]>0) a[link[i]]=i; // printf("%d ",link[i]);}  for(i=1;i<=n;i++){    printf("%d",a[i]);if(i!=n) printf(" ");else printf("\n");}}return 0;}


原创粉丝点击