-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
145 lines (131 loc) · 2.22 KB
/
main.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <random>
#include <cstdlib>
#include <algorithm>
#include "graph.cpp"
#include "mincut.cpp"
#include "tworespectingtrees.cpp"
using namespace std;
// Read graph from stdin
// Format:
// n m
// u_1 v_1 w_1
// u_2 v_2 w_2
// .
// .
// .
// u_m v_m w_m
pgraph graph_reader()
{
int n, m;
scanf("%d%d", &n, &m);
vector<pedge> E;
for(int i = 0;i < m;i++)
{
int u, v;
double c;
scanf("%d%d%lf", &u, &v, &c);
//u--, v--;
pedge tmp = new edge(u, v, i, c);
E.emplace_back(tmp);
}
pgraph res = new graph(n, m, E);
return res;
}
// Read tree from stdin
// Format:
// n
// u_1 v_1 idx_1 w_1
// u_2 v_2 idx_2 w_2
// .
// .
// .
// u_{n-1} v_{n-1} idx_{n-1} w_{n-1}
ptree tree_reader()
{
int n;
scanf("%d", &n);
vector<pedge> E;
for(int i = 0;i < n-1;i++)
{
int u, v, idx;
double c;
scanf("%d%d%d%lf", &u, &v, &idx, &c);
//u--, v--;
//cout << u << " " << v << " " << c << "\n";
pedge tmp = new edge(u, v, idx, c);
E.emplace_back(tmp);
}
ptree res = new tree(n, E);
return res;
}
void tester(pgraph G)
{
double d;
scanf("%lf",&d);
vector<ptree> trees = tworespectingtrees(d, G);
printf("%d\n", int(trees.size()));
// Fix wts using edge index
for(auto it: trees)
{
for(auto gt: it->E)
{
gt->w = G->E[gt->idx]->w;
//cout << gt->u << " " << gt->v << " " << gt->idx << " " << gt->w << "\n";
}
//cout << "\n";
}
//vector<ptree> trees;
//trees.push_back(tree_reader());
double res = double(1e9)+5;
for(auto it: trees)
{
mincut mc(G, it);
res = min(res, mc.compute());
mc.clear();
}
for(auto it: trees)
{
for(auto gt: it->E) delete(gt);
delete(it);
}
for(auto it: G->E) delete(it);
delete(G);
printf("%lf\n", res);
}
void tester_lemma8ds()
{
ptree T = tree_reader();
lemma8ds D(T);
int m;
scanf("%d", &m);
while(m--)
{
int t, u, v;
double x;
scanf("%d", &t);
if(t == 0) printf("%lf\n", D.QueryMinimum());
else if(t == 1)
{
scanf("%d%d%lf", &u, &v, &x);
D.PathAdd(u, v, x);
}
else if(t == 2)
{
scanf("%d%d%lf", &u, &v, &x);
D.NonPathAdd(u, v, x);
}
}
}
int main(void)
{
srand(time(NULL));
pgraph G = graph_reader();
tester(G);
//tester2();
}