QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760715#5811. Wi-fi Towersrealcomplex028 ✓17ms3896kbC++203.6kb2024-11-18 18:39:482024-11-18 18:39:49

Judging History

你现在查看的是最新测评结果

  • [2024-11-18 18:39:49]
  • 评测
  • 测评结果:28
  • 用时:17ms
  • 内存:3896kb
  • [2024-11-18 18:39:48]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

struct FlowEdge {
    int v, u;
    ll cap, flow = 0;
    FlowEdge(int v, int u, ll cap) : v(v), u(u), cap(cap) {}
};

struct Dinic {
    const ll flow_inf = (ll)1e18;
    vector<FlowEdge> edges;
    vector<vector<int>> adj;
    int nn, mm = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;
    Dinic(int nn, int s, int t) : nn(nn), s(s), t(t) {
        adj.resize(nn);
        level.resize(nn);
        ptr.resize(nn);
    }
    void add_edge(int v, int u, ll cap){
        edges.push_back({v, u, cap});
        edges.push_back({u, v, 0});
        adj[v].push_back(mm);
        adj[u].push_back(mm + 1);
        mm += 2;
    }
    bool bfs() {
        while(!q.empty()){
            int v = q.front();
            q.pop();
            for(int id : adj[v]){
                if(edges[id].cap - edges[id].flow < 1)
                    continue;
                if(level[edges[id].u] != -1)
                    continue;
                level[edges[id].u] = level[v] + 1;
                q.push(edges[id].u);
            }
        }
        return level[t] != -1;
    }
    ll dfs(int v, ll pushed){
        if(pushed == 0)
            return 0;
        if(v == t)
            return pushed;
        for(int &cid = ptr[v]; cid < (int)adj[v].size(); cid ++ ){
            int id = adj[v][cid];
            int u = edges[id].u;
            if(level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
                continue;
            ll tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
            if(tr == 0)
                continue;
            edges[id].flow += tr;
            edges[id ^ 1].flow -= tr;
            return tr;
        }
        return 0;
    }
    ll flow(){
        ll f = 0;
        while(true){
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            q.push(s);
            if(!bfs())
                break;
            fill(ptr.begin(), ptr.end(), 0);
            while(ll pushed = dfs(s, flow_inf)){
                f += pushed;
            }
        }
        return f;
    }
    vector<bool> vis;
    void dfs_t(int node){
        if(vis[node]) return;
        vis[node]=true;
        for(auto x : adj[node]){
            if(edges[x].cap - edges[x].flow > 0){
                dfs_t(edges[x].u);
            }
        }
    }
};

const int N = (int)505;
int x[N], y[N], s[N], r[N];

void solve(int idx){
    int n;
    cin >> n;
    Dinic dd(2 * n + 2, 0, 2 * n + 1);
    int profit = 0;
    for(int i = 1; i <= n; i ++ ){
        cin >> x[i] >> y[i] >> r[i] >> s[i];
        if(s[i] > 0){
            profit += s[i];
            dd.add_edge(n + i, 2 * n + 1, s[i]);
        }
        else if(s[i] < 0){
            dd.add_edge(0, i, -s[i]);
        }
        dd.add_edge(i, n + i, (ll)1e18);
        dd.add_edge(n + i, i, (ll)1e18);
    }
    for(int i = 1; i <= n; i ++ ){
        for(int j = 1; j <= n; j ++ ){
            if(j == i) continue;
            if((x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]) <= (r[i] * r[i]))
            dd.add_edge(n + j, i, (ll)1e18);
        }
    }
    cout << "Case #" << idx << ": " << profit - dd.flow() << "\n";
}

int main(){
    fastIO;
    int tc;
    cin >> tc;
    for(int iq = 1; iq <= tc; iq ++ )
        solve(iq);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 1ms
memory: 3596kb

input:

55
15
10 12 45 -1
9 5 50 1
5 20 41 1
11 8 50 1
20 13 44 1
2 0 45 -1
5 4 47 -1
14 13 42 -1
19 1 47 -1
8 0 41 -1
8 16 40 -1
15 11 42 1
8 8 42 1
2 9 50 1
0 10 44 1
15
872 2191 2 -386
-2071 -3796 1 -568
4237 -5305 6742 275
-2307 -6269 1 -148
6875 -19 461 -519
-9871 -3500 64 -306
-2576 -5709 25 686
431 8...

output:

Case #1: 1
Case #2: 3091
Case #3: 923
Case #4: 651
Case #5: 1387
Case #6: 1319
Case #7: 1
Case #8: 747
Case #9: 0
Case #10: 1443
Case #11: 2
Case #12: 0
Case #13: 1746
Case #14: 1533
Case #15: 687
Case #16: 1851
Case #17: 2140
Case #18: 886
Case #19: 673
Case #20: 0
Case #21: 1772
Case #22: 0
Case #...

result:

ok 55 lines

Subtask #2:

score: 25
Accepted

Test #2:

score: 25
Accepted
time: 17ms
memory: 3896kb

input:

55
5
0 -1 7 10
0 1 7 10
5 0 1 -15
10 0 6 10
15 1 2 -20
171
46 50 3 3
30 6 8 2
14 30 8 2
10 10 8 4
48 28 1 -2
18 18 9 3
52 12 1 -2
54 6 10 4
26 54 10 4
24 36 1 -1
30 14 3 1
4 36 1 -1
40 4 1 -2
14 14 9 4
52 40 1 -3
20 48 1 -2
50 6 8 3
16 16 1 -3
10 42 3 2
50 38 7 3
44 52 1 -2
4 48 1 -2
16 52 1 -1
32 5...

output:

Case #1: 5
Case #2: 24
Case #3: 18455
Case #4: 46
Case #5: 34885
Case #6: 12211
Case #7: 4653
Case #8: 3560
Case #9: 16236
Case #10: 64
Case #11: 12885
Case #12: 18102
Case #13: 17
Case #14: 10
Case #15: 3824
Case #16: 2589
Case #17: 3949
Case #18: 1
Case #19: 0
Case #20: 28
Case #21: 40141
Case #22...

result:

ok 55 lines

Extra Test:

score: 0
Extra Test Passed