static int M, N; //M, N分别表示左、右侧集合的元素数量
static int[][] Map = new int[100][]; //邻接矩阵存图
static int[] p = new int[100]; //记录当前右侧元素所对应的左侧元素
static bool[] vis = new bool[100]; //记录右侧元素是否已被访问过
static bool match(int i)
{
for (int j = 1; j <= N; ++j)
if (Map[i][j] != 0 && !vis[j]) //有边且未访问
{
vis[j] = true; //记录状态为访问过
if (p[j] == 0 || match(p[j])) //如果暂无匹配,或者原来匹配的左侧元素可以找到新的匹配
{
p[j] = i; //当前左侧元素成为当前右侧元素的新匹配
return true; //返回匹配成功
}
}
return false; //循环结束,仍未找到匹配,返回匹配失败
}
static void Hungarian()
{
int cnt = 0;
for (int i = 1; i <= M; ++i)
{
vis = vis.Select(x => false).ToArray();
if (match(i))
cnt++;
}
}
public void ds()
{
Map = Map.Select(x => new int[100]).ToArray();
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "server=.;database=DCS;uid=sa;pwd=111";
sqlconn.Open();
string cmdtxt1 = "select * from volunteer";
string cmdtxt2 = "select * from tvolunteer";
SqlDataAdapter dap1 = new SqlDataAdapter(cmdtxt1, sqlconn);
SqlDataAdapter dap2 = new SqlDataAdapter(cmdtxt2, sqlconn);
DataSet dataset1 = new DataSet();
DataSet dataset2 = new DataSet();
dap1.Fill(dataset1);
dap2.Fill(dataset2);
string t1 = "select count(*) from volunteer";
string t2 = "select count(*) from tvolunteer";
int x1 = (int)SqlDBHelper.ExecuteScalar(t1);
int x2 = (int)SqlDBHelper.ExecuteScalar(t2);
#endregion
int[,] leader = new int[x1, 3]; //x1个一维数组 ,每个一维数组有3个元素
for(int i = 0;i<dataset1.Tables[0].Rows.Count;i++)
{
// int[] a = new int[3];
for (int x = 1; x < 4;x++ )
{
//a[x-1] = Convert.ToInt32(dataset.Tables[0].Rows[i][x]);
leader[i, x - 1] = Convert.ToInt32(dataset1.Tables[0].Rows[i][x]); { }
}
}
int[,] teacher = new int[x2, 2]; //x2个一维数组 ,每个一位数组有3个元素
for(int i = 0;i<dataset2.Tables[0].Rows.Count;i++)
{
// int[] a = new int[3];
for (int x = 1; x < 3;x++ )
{
//a[x-1] = Convert.ToInt32(dataset.Tables[0].Rows[i][x]);
teacher[i, x - 1] = Convert.ToInt32(dataset2.Tables[0].Rows[i][x]);
}
}
M = leader.GetLength(0);
N = teacher.GetLength(0);
for (int i = 0; i <leader.GetLength(0); i++) //行数
for (int j = 0; j <leader.GetLength(1); j++) //列数
{
Map[i+1][leader[i, j]]++;
}
for (int i = 0; i < teacher.GetLength(0); i++)
for (int j = 0; j <teacher.GetLength(1); j++)
{
Map[teacher[i, j]][i+1]++;
}
Map = Map.Select(x => x.Select(y => y == 2 ? 1 : 0).ToArray()).ToArray();
Hungarian();
for (int r = 0; r < M; r++)
{
int i = p[r];
}
算法代码是某大佬写的,我改了输入数据后出现了越界问题,输入数据的值在截图中,自己水平不够找不出是哪里的问题,求个大佬解惑