kn算法c++转matlab

「已注销」 2018-05-31 01:10:14
[code=c][#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<malloc.h>
using namespace std;
typedef long long ll;
const int maxn = 310;
const int INF = 0x3f3f3f3f;
int nx,ny;//两边的点数
int g[maxn][maxn]= {
{0,0,0,0,0,0,
},
{ 0,3,5,5,4,1,
},
{
0,2,2,0,2,2,
},
{
0,2,4,4,1,0,
},
{
0,0,1,1,0,0,
},
{
0,1,2,1,3,3,

}};;//二分图描述
int linker[maxn],lx[maxn],ly[maxn];//y中各点匹配状态,x,y中的点标号
int slack[maxn];
bool visx[maxn],visy[maxn];
bool DFS(int x)
{
visx[x] = true;
for(int y = 1; y <= ny; y++)
{
if(visy[y])continue;
int tmp = lx[x] + ly[y] - g[x][y];
if(tmp == 0)
{
visy[y] = true;
if(linker[y] == -1 || DFS(linker[y]))
{
linker[y] = x;
return true;
}
}
else if(slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
int KM()
{
memset(linker,-1,sizeof(linker));
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
for(int i = 1; i <= nx; i++)
{
lx[i] = -INF;
for(int j = 1; j <= ny; j++)
if(g[i][j] > lx[i])
lx[i] = g[i][j];
}
for(int x = 1; x <= nx; x++)
{
for(int i = 1; i <= ny; i++)
slack[i] = INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x))break;
int d = INF;
for(int i = 1; i <= ny; i++)
if(!visy[i] && d > slack[i])
d = slack[i];
for(int i = 1; i <= nx; i++)
if(visx[i])
lx[i] -= d;
for(int i = 1; i <= ny; i++)
{
if(visy[i])ly[i] += d;
else slack[i] -= d;
}
}
}
int res = 0;
for(int i = 1; i <= nx; i++)
if(linker[i] != -1)
res += g[linker[i]][i];
return res;
}
int main()
{
int n;
n = 5;
{

nx = ny = n;
printf("%d\n",KM());
}
re/code]
matlab 代码
main.m

g= [0,0,0,0,0,0;0,3,5,5,4,1;0,2,2,0,2,2;0,2,4,4,1,0;0,0,1,1,0,0;0,1,2,1,3,3];
n = 5;
nx = n;
ny = n;
for i = 1: 10000
linker(i) = -1;
end

for i = 1: 10000
lx(i) = 0;
end
%lx[]=0;

for i = 1: 10000
ly(i) = 0;
end
%ly[] = 0;
%for( i = 1; i <= nx; i++)
for i = 1:nx

lx(i) = -1 * inf;
% for(int j = 1; j <= ny; j++)
for j = 1: ny
if(g(i,j) > lx(i))
lx(i) = g(i,j);
end
end
end
for x =1 :nx
%(int x = 1; x <= nx; x++)
for i =1 :ny
slack(i) = inf;
end
while 1

for i =1 : 1000
visx(i) = 0;
end
for i =1: 1000
visy(i) = 0;
end
c =dfs(x);
if c
,break
end;
d = inf;
for i =1 :ny
if(visy(i) == 0 && d > slack(i) )
d = slack(i)
end
end
% for(int i = 1; i <= ny; i++)
% if(!visy[i] && d > slack[i])
% d = slack[i];
for i =1 :nx
if(visx(i))
lx(i) =lx(i) - d;
end
end
%for(int i = 1; i <= nx; i++)
% if(visx[i])
% lx[i] -= d;
for i =1 : ny
if(visy(i)) ly(i) = ly(i) +d;
else slack(i) = slack(i) -d;
end
end
end
end
res = 0;
for i =1 :nx
if(linker(i) ~= -1)
res =res +g(linker(i),i);
end
end

fprintf('%d\n',res);
dfs.m
function x = dfs(x)

nx =5;
ny =5;
visx(x) = true;
for y = 1:ny
% for(int y = 1; y <= ny; y++)

if visy(y)continue; end
tmp = lx(x) + ly(y) - g(x,y);
if(tmp == 0)

visy(y) = true;
if(linker(y) == -1 || dfs(linker(y)))

linker(y) = x;
x =true; %? 返回值怎么写?
end
end

if(slack(y) > tmp)
slack(y) = tmp;
end
end
% return false;
x = false;
...全文
883 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧