ACdream 1227Beloved Sons【二分图最佳匹配】

Description

      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."

     Suddenly you recalled that not so long ago the king told you about each of his sons, so you knew how much he loves him. So you decided to please the king and make such a marriage list that the king would be most happy. You know that the happiness of the king will be proportional to the square root of the sum of the squares of his love to the sons that would marry the girls they like.

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

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

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

错过了下午的比赛,学弟说这个题他们默认我会图论,就贴了模板过的,心虚晚上回来补题,50min1A(最快的队伍都是1个小时出头才出的,哼哼)

题意:依旧是给王子们选心爱的女孩,给出男孩子们分别喜欢的女生,以及国王对于每个王子的偏爱值,最终的结果是得到自己喜欢女孩子的王子的偏爱值的平方和,要求这个值最大。输出每个男孩子对应选择女生的标号。

做法:从模板可以看出link[j]=i表示女生j与王子i配对,那么如果配对的不是王子喜欢的输出0

    #include <stdio.h>
    #include <string.h>
    #define M 410
    #define inf 0x3f3f3f3f
    using namespace std;
    int abs(int x)
    {
        return x>0?x:-x;
    }
    int n,m,nx,ny;
    int link[M],lx[M],ly[M],slack[M];///lx,ly为顶标,nx,ny分别为x点集y点集的个数
    int visx[M],visy[M],w[M][M];

    int DFS(int x)
    {
        visx[x] = 1;
        for (int y = 1; y <= ny; y ++)
        {
            if (visy[y]) continue;
            int t = lx[x] + ly[y] - w[x][y];
            if (t == 0)
            {
                visy[y] = 1;
                if (link[y] == -1||DFS(link[y]))
                {
                    link[y] = x;
                    return 1;
                }
            }
            else if (slack[y] > t)  ///不在相等子图中slack 取最小的
                slack[y] = t;
        }
        return 0;
    }
    int KM()
    {
        int i,j;
        memset (link,-1,sizeof(link));
        memset (ly,0,sizeof(ly));
        for (i = 1; i <= nx; i ++)          ///lx初始化为与它关联边中最大的
            for (j = 1,lx[i] = -inf; j <= ny; j ++)
                if (w[i][j] > lx[i])
                    lx[i] = w[i][j];

        for (int x = 1; x <= nx; x ++)
        {
            for (i = 1; i <= ny; i ++)
                slack[i] = inf;
            while (1)
            {
                memset (visx,0,sizeof(visx));
                memset (visy,0,sizeof(visy));
                if (DFS(x))     ///若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
                    break;  ///若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
                ///方法为:将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
                ///所有在增广轨中的Y方点的标号全部加上一个常数d
                int d = inf;
                for (i = 1; i <= ny; i ++)
                    if (!visy[i]&&d > slack[i])
                        d = slack[i];
                for (i = 1; i <= nx; i ++)
                    if (visx[i])
                        lx[i] -= d;
                for (i = 1; i <= ny; i ++) ///修改顶标后,要把所有不在交错树中的Y顶点的slack值都减去d
                    if (visy[i])
                        ly[i] += d;
                    else
                        slack[i] -= d;
            }
        }
        int res = 0;
        int tmp[M];
        for (i = 1; i <= ny; i ++)
        {
            if (link[i] > -1)
            {
                for(int j=1;j<=nx;j++)
                {
                    if(link[i]==j)
                    {
                        if(w[link[i]][i]!=0)
                            tmp[j]=i;
                        else tmp[j]=0;
                        break;
                    }
                }
                res += w[link[i]][i];
            }
         //   else printf("0 ");
        }
        for(int i=1;i<nx;i++)printf("%d ",tmp[i]);
        printf("%d\n",tmp[nx]);
        //puts("");
     //   printf("res=%d\n",res);
        return res;
    }
    int cost[M];
    int main()
    {
      //  freopen("cin.txt","r",stdin);
        int n;
        while(~scanf("%d",&n))
        {
            nx=ny=n;
            memset(w,0,sizeof(w));
            for(int i=1;i<=n;i++)scanf("%d",&cost[i]);
            for(int i=1;i<=n;i++)
            {
                int k,a;
                scanf("%d",&k);
                while(k--)
                {
                    scanf("%d",&a);
                    w[i][a]=cost[i]*cost[i];
                }
            }
            KM();
        }
        return 0;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值