Beloved Sons(zju1338,KM)

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 510
int 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;
}


好的,我可以帮你使用Python编写一个GUI界面来运行这段代码。我将使用Tkinter库来创建GUI界面,并将这段代码嵌入到GUI中。 首先,需要安装Tkinter库。在终端或命令提示符中输入以下命令来安装: ``` pip install tkinter ``` 接下来,我们将使用以下代码来创建GUI界面: ```python import tkinter as tk import socket import time class GUI: def __init__(self, master): self.master = master master.title("UDP Socket Chat") # IP and Port Labels ip_label = tk.Label(master, text="IP Address:") ip_label.grid(row=0, column=0) port_label = tk.Label(master, text="Port:") port_label.grid(row=1, column=0) # IP and Port Entries self.ip_entry = tk.Entry(master) self.ip_entry.grid(row=0, column=1) self.ip_entry.insert(0, "192.168.185.60") self.port_entry = tk.Entry(master) self.port_entry.grid(row=1, column=1) self.port_entry.insert(0, "8888") # Chat Log self.log = tk.Text(master, width=50, height=20) self.log.grid(row=2, column=0, columnspan=2) # Send Message Entry self.message_entry = tk.Entry(master, width=40) self.message_entry.grid(row=3, column=0, padx=5, pady=5) # Send Message Button self.send_button = tk.Button(master, text="Send", command=self.send_message) self.send_button.grid(row=3, column=1, padx=5, pady=5) # Quit Button self.quit_button = tk.Button(master, text="Quit", command=master.quit) self.quit_button.grid(row=4, column=1) # UDP Socket self.byte = 1024 self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udp_socket.bind((self.ip_entry.get(), int(self.port_entry.get()))) # Start Listening Thread self.listen_thread = ListenThread(self.udp_socket, self.log) self.listen_thread.start() def send_message(self): message = self.message_entry.get().encode("utf-8") self.udp_socket.sendto(message, (self.ip_entry.get(), int(self.port_entry.get()))) self.message_entry.delete(0, tk.END) class ListenThread(threading.Thread): def __init__(self, udp_socket, log): threading.Thread.__init__(self) self.udp_socket = udp_socket self.log = log def run(self): while True: recv_data, other_addr = self.udp_socket.recvfrom(self.byte) localTime = time.asctime(time.localtime(time.time())) message = localTime + " receive message from %s: %s" % (other_addr, recv_data.decode("utf-8")) self.log.insert(tk.END, message + "\n") self.log.see(tk.END) root = tk.Tk() gui = GUI(root) root.mainloop() ``` 这个GUI界面包含了一个IP地址和端口号的输入框,一个聊天记录框,一个发送消息的输入框和一个发送按钮。当用户点击发送按钮时,程序将把输入框中的消息发送给指定的IP地址和端口号。 在GUI的构造函数中,创建了一个UDP Socket,并启动了一个监听线程,该线程将接收来自其他客户端的消息并将其显示在聊天记录框中。 现在,您可以保存这个代码并运行它。当您运行它时,您应该会看到一个窗口,您可以在其中输入IP地址和端口号,并开始与其他客户端进行聊天。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值