-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy.cpp
More file actions
102 lines (80 loc) · 1.94 KB
/
greedy.cpp
File metadata and controls
102 lines (80 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<utility>
#include<stack>
#include<map>
#define MAX_V 1000
#define MAX_U 1000
#define MAX_UV 2000
#define INF 2e9
#define min(a,b) a<b ? a : b
using namespace std;
typedef pair< double, pair<int,int> > edge;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
double c[MAX_V][MAX_U];
class Greedy{
public:
int M[MAX_UV], visited[MAX_UV];
int n;
vector< edge > q;
Greedy(){
scanf("%d",&n);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%lf",&c[i][j]);
q.push_back(edge(c[i][j], make_pair(i,j)));
}
M[i] = M[i+n] = -1;
}
}
void printM(){
for(int i=0; i<n; i++){
printf("%d %d\n", i, M[i]-n);
}
}
void printC(){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%.6lf%s", c[i][j], (j<n-1 ? "\t" : ""));
}
printf("\n");
}
}
double matchingValue(){
double sum = 0.0;
for(int i=0; i<n; i++)
if(M[i] != -1)
sum += c[i][M[i]-n];
return sum;
}
void match(void){
sort(q.begin(), q.end());
vector< edge >::iterator it;
int m_size;
for(it = q.begin(), m_size=0; it != q.end() && m_size<n; it++){
int v = it->second.first;
int u = it->second.second + n;
if(M[v] == -1 && M[u] == -1){
M[v] = u;
M[u] = v;
m_size++;
}
}
}
};
int main(){
Greedy g;
g.match();
g.printM();
printf("\n%lf\n",g.matchingValue());
return 0;
}