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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #include <bits/stdc++.h>
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops")
#define MAX_N 444 #define ll long long #define pll pair<ll, ll> #define vpll vector<pll> #define vall3 vector<array<ll,3>> #define all5 array<ll,5> #define vall5 vector<all5> #define vll vector<ll> #define vs vector<string> #define usll unordered_set<ll> #define vvs vector<vs> #define vvll vector<vll> #define all(a) begin(a), end(a) using namespace std;
ll source = 441, sink = 442; vall5 edges; ll cap[MAX_N][MAX_N], cost[MAX_N][MAX_N], flo[MAX_N][MAX_N], path[MAX_N]; vll adj[MAX_N];
ll ccw(pll& a, pll& b, pll& c) { ll res = a.first * b.second + b.first * c.second + c.first * a.second; res -= (a.second * b.first + b.second * c.first + c.second * a.first); if(res > 0) return 1; if(res == 0) return 0; return -1; }
bool intersect(all5& x, all5& y) { pll a = {x[0], x[1]}; pll b = {x[2], x[3]}; pll c = {y[0], y[1]}; pll d = {y[2], y[3]};
ll ab = ccw(a, b, c) * ccw(a, b, d); ll cd = ccw(c, d, a) * ccw(c, d, b);
if(ab == 0 and cd == 0) { if(a > b) swap(a, b); if(c > d) swap(c, d); return c <= b and a <= d; } return ab <= 0 and cd <= 0; }
void connect(pll u, pll v) { adj[u.first].push_back(v.first); adj[v.first].push_back(u.first);
cap[u.first][v.first] = 1;
cost[u.first][v.first] = -u.second * v.second; cost[v.first][u.first] = u.second * v.second; }
bool bfs(ll u, ll v) { bool inc[MAX_N]; vll w(MAX_N, 987654321); queue<ll> q; memset(path, 0, sizeof path); memset(inc, 0, sizeof inc);
inc[u] = true; q.push(u); w[u] = 0;
while(!q.empty()) { auto n = q.front(); q.pop(); inc[n] = false; for(auto& m : adj[n]) { if(cap[n][m] - flo[n][m] > 0 and w[n] + cost[n][m] < w[m]) { w[m] = w[n] + cost[n][m]; path[m] = n; if(!inc[m]) { inc[m] = true; q.push(m); } } } }
return path[v] != 0; }
pll solve(ll n, ll m) { for(ll i = 0; i < n; i++) { for(ll j = n; j < n + m; j++) { if(intersect(edges[i], edges[j])) { connect({i + 1, edges[i][4]}, { j + 1, edges[j][4]}); } } }
for(ll i = 0; i < n; i++) { connect({source, 0}, {i + 1, 0}); } for(ll i = n; i < n + m; i++) { connect({i + 1, 0}, {sink, 0}); } ll pick = 0, weight = 0; while(bfs(source, sink)) { ll v = sink, mi = INT_MAX, sum = 0; while(source != v) { ll u = path[v]; mi = min(mi, cap[u][v] - flo[u][v]); v = u; }
v = sink; while(source != v) { ll u = path[v]; sum += mi * cost[u][v]; flo[u][v] += mi; flo[v][u] -= mi;
v = u; }
pick++; weight += sum;
} return {pick, -weight}; }
int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.setf(ios::fixed); cout.precision(6); ll tc, n, m;
cin>>tc; while(tc--) { cin>>n>>m; edges.clear(); for(ll i = 0; i < MAX_N; i++) adj[i].clear(); memset(flo, 0, sizeof flo); memset(cap, 0, sizeof cap); memset(cost, 0, sizeof cost); for(ll i = 1,a,b,c,d,e; i <= n + m; i++) { cin>>a>>b>>c>>d>>e; edges.push_back({a,b,c,d,e}); }
auto [a, b] = solve(n, m); cout<<a<<' '<<b<<'\n'; }
return 0; }
|