1
#include <iostream>
2
using namespace std;
3![]()
4
#define max 100
5![]()
6
int matrix[max][max]; //关联矩阵
7![]()
8
int x,y; //|V1|,|V2|
9![]()
10
int check[max]; //确定下部图是否被访问
11
int link[max]; //下部图连接上部图的点
12![]()
13
bool find(int up)
14
{
15
int down;
16
for(down=0;down<y;down++)
17
{
18
if(matrix[up][down]==1&&check[down]==0)
19
{
20
check[down]=1;
21
if(link[down]==-1||find(link[down]))
22
{
23
link[down]=up;
24
return true;
25
}
26
}
27
}
28
return false;
29
}
30![]()
31
int match()
32
{
33
int up;
34
int num=0;
35
memset(link,-1,sizeof(link));
36
for(up=0;up<x;up++)
37
{
38
memset(check,0,sizeof(check));
39
if(find(up))
40
num++;
41
}
42
return num;
43
}
44![]()
45
int main()
46
{
47
int i,j;
48
int r;
49
cout<<"使用说明:"<<endl
50
<<"首先输入二部图上部和下部的节点个数;"<<endl
51
<<"然后输入关联矩阵,1表示匹配,0表示不匹配;"<<endl
52
<<"程序会给出最大匹配数和匹配路径"<<endl<<endl;
53
cout<<"依次输入|V1|,|V2|:"<<endl;
54
cin>>x>>y;
55
56
cout<<"输入关联矩阵:"<<endl;
57
for(i=0;i<x;i++)
58
for(j=0;j<y;j++)
59
cin>>matrix[i][j];
60
r=match();
61
cout<<"最大匹配数: ";
62
cout<<r<<endl;
63
64
cout<<"匹配路径:"<<endl;
65
for(i=0;i<y;i++)
66
{
67
if(link[i]!=-1)
68
cout<<"Y"<<i+1<<"-X"<<link[i]+1<<" ";
69
}
70
cout<<endl;
71
return 0;
72
}
===================================================
#include <iostream>2
using namespace std;3

4
#define max 1005

6
int matrix[max][max]; //关联矩阵7

8
int x,y; //|V1|,|V2|9

10
int check[max]; //确定下部图是否被访问11
int link[max]; //下部图连接上部图的点12

13
bool find(int up)14
{15
int down;16
for(down=0;down<y;down++)17
{18
if(matrix[up][down]==1&&check[down]==0)19
{20
check[down]=1;21
if(link[down]==-1||find(link[down]))22
{23
link[down]=up;24
return true;25
}26
}27
}28
return false;29
}30

31
int match()32
{33
int up;34
int num=0;35
memset(link,-1,sizeof(link));36
for(up=0;up<x;up++)37
{38
memset(check,0,sizeof(check));39
if(find(up))40
num++;41
}42
return num;43
}44

45
int main()46
{47
int i,j;48
int r;49
cout<<"使用说明:"<<endl50
<<"首先输入二部图上部和下部的节点个数;"<<endl51
<<"然后输入关联矩阵,1表示匹配,0表示不匹配;"<<endl52
<<"程序会给出最大匹配数和匹配路径"<<endl<<endl;53
cout<<"依次输入|V1|,|V2|:"<<endl;54
cin>>x>>y;55
56
cout<<"输入关联矩阵:"<<endl;57
for(i=0;i<x;i++)58
for(j=0;j<y;j++)59
cin>>matrix[i][j];60
r=match();61
cout<<"最大匹配数: ";62
cout<<r<<endl;63
64
cout<<"匹配路径:"<<endl;65
for(i=0;i<y;i++)66
{67
if(link[i]!=-1)68
cout<<"Y"<<i+1<<"-X"<<link[i]+1<<" ";69
}70
cout<<endl;71
return 0;72
} 1
#include <iostream>
2
#include <vector>
3
#include <string>
4
using namespace std;
5![]()
6
///////////////////////////////////////////////////////////////////
7
// 节点类
8
class Node
9
{
10
public:
11
Node * left;
12
Node * right;
13
double num;
14
string source;
15
string code;
16![]()
17
Node();
18
~Node();
19
};
20![]()
21
Node::Node()
22
{
23
left=NULL;
24
right=NULL;
25
num=0;
26
source="";
27
code="";
28
}
29![]()
30
Node::~Node(){}
31![]()
32
///////////////////////////////////////////////////////////////////
33![]()
34
//排序
35
void sort(vector<Node> &a)
36
{
37
int i,j;
38
for(i=0;i<a.size();i++)
39
for(j=0;j<a.size()-i-1;j++)
40
{
41
if(a[j].num>a[j+1].num)
42
swap(a[j],a[j+1]);
43
}
44
}
45![]()
46
/////////////////////////////////////////////////////////////////////
47
//Huffman编码
48
void code(Node *root)
49
{
50
if(root->left==NULL)
51
return;
52
else
53
root->left->code=root->code+"0";
54
if(root->right==NULL)
55
return;
56
else
57
root->right->code=root->code+"1";
58![]()
59
code(root->left);
60
code(root->right);
61![]()
62
}
63
////////////////////////////////////////////////////////////////////////
64
//输出编码好的对象
65
void printCode(Node *root)
66
{
67
Node *temp;
68
temp=root;
69
if(temp->left==NULL||temp->right==NULL)
70
{
71
cout<<temp->source<<" "<<temp->num<<" "<<temp->code<<endl;
72
return;
73
}
74
printCode(temp->left);
75
printCode(temp->right);
76
}
77![]()
78
/////////////////////////////////////////////////////////////////////////
79
//竖向打印树,看的时候横着看
80
void PrintTree(Node *bt,int nLayer)
81
{
82
Node *temp;
83
temp=bt;
84
if(temp==NULL) return;
85
PrintTree(temp->right, nLayer+4);
86
for(int i=0;i<nLayer;i++)
87
cout<<" ";
88
cout<<temp->source<<" "<<temp->num<<" "<<temp->code<<endl;
89
PrintTree(bt->left, nLayer+4);
90
}
91
///////////////////////////////////////////////////////////////////////////
92
int main()
93
{
94
int size;
95
int i;
96
Node root;
97
cout<<"输入需要编码的节点个数:"<<endl;
98
cin>>size;
99
vector<Node>a(size);
100
101
cout<<"依次输入需要编码的字符和权值:"<<endl;
102
for(i=0;i<size;i++)
103
{
104
cout<<"#"<<i+1<<":"<<endl;
105
cin>>a[i].source>>a[i].num;
106
}
107
for(i=0;i<2*size-2;i=i+2)
108
{
109
Node temp;
110
sort(a);
111
temp.num=a[i].num+a[i+1].num;
112
temp.source=a[i].source+a[i+1].source;
113
a.push_back(temp);
114
a[a.size()-1].left=&a[i];
115
a[a.size()-1].right=&a[i+1];
116
root=a[a.size()-1];
117
}
118
code(&root);
119
120
cout<<"Huffman树,从左到右依次是字符,权值,密码,看的时候横着看"<<endl<<endl;
121
cout<<"HuffmanTree:"<<endl;
122
PrintTree(&root,0);
123
cout<<endl;
124
cout<<"密码与对应的字符及权值:"<<endl;
125
printCode(&root);
126
return 0;
127
}
128![]()
129![]()
#include <iostream>2
#include <vector>3
#include <string>4
using namespace std;5

6
///////////////////////////////////////////////////////////////////7
// 节点类8
class Node9
{10
public:11
Node * left;12
Node * right;13
double num;14
string source;15
string code;16

17
Node();18
~Node();19
};20

21
Node::Node()22
{23
left=NULL;24
right=NULL;25
num=0;26
source="";27
code="";28
}29

30
Node::~Node(){}31

32
///////////////////////////////////////////////////////////////////33

34
//排序35
void sort(vector<Node> &a)36
{37
int i,j;38
for(i=0;i<a.size();i++)39
for(j=0;j<a.size()-i-1;j++)40
{41
if(a[j].num>a[j+1].num)42
swap(a[j],a[j+1]);43
}44
}45

46
/////////////////////////////////////////////////////////////////////47
//Huffman编码48
void code(Node *root)49
{50
if(root->left==NULL)51
return;52
else53
root->left->code=root->code+"0";54
if(root->right==NULL)55
return;56
else57
root->right->code=root->code+"1";58

59
code(root->left);60
code(root->right);61

62
}63
////////////////////////////////////////////////////////////////////////64
//输出编码好的对象65
void printCode(Node *root)66
{67
Node *temp;68
temp=root;69
if(temp->left==NULL||temp->right==NULL)70
{71
cout<<temp->source<<" "<<temp->num<<" "<<temp->code<<endl;72
return;73
}74
printCode(temp->left);75
printCode(temp->right);76
}77

78
/////////////////////////////////////////////////////////////////////////79
//竖向打印树,看的时候横着看80
void PrintTree(Node *bt,int nLayer)81
{82
Node *temp;83
temp=bt;84
if(temp==NULL) return;85
PrintTree(temp->right, nLayer+4);86
for(int i=0;i<nLayer;i++)87
cout<<" ";88
cout<<temp->source<<" "<<temp->num<<" "<<temp->code<<endl;89
PrintTree(bt->left, nLayer+4);90
}91
///////////////////////////////////////////////////////////////////////////92
int main()93
{94
int size;95
int i;96
Node root;97
cout<<"输入需要编码的节点个数:"<<endl;98
cin>>size;99
vector<Node>a(size);100
101
cout<<"依次输入需要编码的字符和权值:"<<endl;102
for(i=0;i<size;i++)103
{104
cout<<"#"<<i+1<<":"<<endl;105
cin>>a[i].source>>a[i].num;106
}107
for(i=0;i<2*size-2;i=i+2)108
{109
Node temp;110
sort(a);111
temp.num=a[i].num+a[i+1].num;112
temp.source=a[i].source+a[i+1].source;113
a.push_back(temp);114
a[a.size()-1].left=&a[i];115
a[a.size()-1].right=&a[i+1];116
root=a[a.size()-1];117
}118
code(&root);119
120
cout<<"Huffman树,从左到右依次是字符,权值,密码,看的时候横着看"<<endl<<endl;121
cout<<"HuffmanTree:"<<endl;122
PrintTree(&root,0);123
cout<<endl;124
cout<<"密码与对应的字符及权值:"<<endl;125
printCode(&root);126
return 0;127
}128

129



浙公网安备 33010602011771号