1 Star 1 Fork 0

adan shaw / structures-algorithms-in-C

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Hungary.c 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
adan-shaw 提交于 2024-02-16 18:32 . update
#include <stdio.h>
#include <string.h>
#define ARRAYSIZE(array) (sizeof array/sizeof *array)
struct Node
{
int x;
int y;
};
int available (struct Node *node1, struct Node *node2)
{
return (node1->x >= node2->x && node1->y >= node2->y);
}
void showresult (int *match, struct Node *pset, int qsize, struct Node *qset)
{
for (int i = 0; i < qsize; i++)
if (match[i] != -1)
printf ("(%d,%d)=>(%d,%d)\n", pset[match[i]].x, pset[match[i]].y, qset[i].x, qset[i].y);
}
#define TRUE 1
#define FALSE 0
int findout (int *match, struct Node *pset, struct Node *qset, int qsize, int *used, int who, int head)
{
int new;
for (int j = head; j < qsize; j++)
if (available (&pset[who], &qset[j]))
{
used[j] = 1;
if (match[j] == -1 || findout (match, pset, qset, qsize, used, match[j], j + 1))
{
match[j] = who;
return TRUE;
}
}
return FALSE;
}
void hungary (struct Node *pset, int psize, struct Node *qset, int qsize)
{
int counter = 0, used[qsize], match[psize];
memset (used, 0, sizeof (int) * qsize);
for (int i = 0; i < qsize; i++)
match[i] = -1;
for (int i = 0; i < psize; i++)
if (findout (match, pset, qset, qsize, used, i, 0))
counter++;
printf ("counter=%d\n", counter);
showresult (match, pset, qsize, qset);
}
int main (void)
{
struct Node pset[] = {
{5, 2}, {4, 1}, {5, 2}, {2, 1}, {2, 3}, {1, 2}, {5, 3}, {4, 5}, {2, 6}, {2, 2}
}, qset[] = {
{3, 5}, {1, 1}, {5, 3}, {2, 3}, {2, 4}, {4, 1}, {3, 1}, {1, 3}, {3, 2}, {3, 2}
};
int psize = ARRAYSIZE (pset), qsize = ARRAYSIZE (qset);
hungary (pset, psize, qset, qsize);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/adan_shaw/structures-algorithms-in-C.git
git@gitee.com:adan_shaw/structures-algorithms-in-C.git
adan_shaw
structures-algorithms-in-C
structures-algorithms-in-C
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891