求助!WA on #2
查看原帖
求助!WA on #2
678858
ShiRoZeTsuHL卜奎BBQ!楼主2023/8/13 10:30

其他都 AC,开了 long long,但是也 WA on #2

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 5;
const int maxm = 1003005;
const ll inf = 0x3f3f3f3f3f3f3f3f;

struct edge {
    int to, nxt; ll w, c;
} e[maxm<<1];

int tot = 1, head[maxn];
void add(int u, int v, ll w, ll c) {
    e[++tot].to = v;
    e[tot].w = w;
    e[tot].c = c;
    e[tot].nxt = head[u];
    head[u] = tot;

    e[++tot].to = u;
    e[tot].w = 0;
    e[tot].c = -c;
    e[tot].nxt = head[v];
    head[v] = tot;
}

int n, k, s, ss, t;
ll h[maxn], dis[maxn];
ll minc;
bool vis[maxn];

void spfa() {
    for(int i = 1; i <= t; i++) h[i] = inf;
    h[s] = 0; vis[s] = true;
    queue<int> q; q.push(s);
    while(!q.empty()) {
        int u = q.front(); q.pop();
        vis[u] = false;
        for(int i = head[u]; i; i = e[i].nxt) {
            int v = e[i].to;
            if(e[i].w && h[v] > h[u]+e[i].c) {
                h[v] = h[u]+e[i].c;
                if(!vis[v]) {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
}

struct node {
    int u; ll w;
    bool operator < (const node& p) const {
        return w > p.w;
    }
};

struct pedge {
    int to, e;
} p[maxn];

bool dijkstra() {
    for(int i = 1; i <= t; i++) dis[i] = inf;
    for(int i = 1; i <= t; i++) vis[i] = false;
    dis[s] = 0;
    priority_queue<node> q; q.push((node){s, 0});
    while(!q.empty()) {
        int u = q.top().u; q.pop();
        if(vis[u]) continue;
        vis[u] = true;
        for(int i = head[u]; i; i = e[i].nxt) {
            int v = e[i].to; ll nc = h[u]+e[i].c-h[v];
            if(e[i].w && dis[v] > dis[u]+nc) {
                dis[v] = dis[u]+nc;
                p[v].to = u;
                p[v].e = i;
                if(!vis[v]) q.push((node){v, dis[v]});
            }
        }
    }
    return dis[t] != inf;
}

struct seg {
    ll l, r, len;
} a[505];

int main() {
    scanf("%d %d", &n, &k);
    s = (n<<1)+1; ss = (n<<1)+2; t = (n<<1)+3;
    add(s, ss, k, 0);

    for(int i = 1; i <= n; i++) {
        ll b, d;
        scanf("%lld %lld %lld %lld", &a[i].l, &b, &a[i].r, &d);
        if(a[i].l > a[i].r) swap(a[i].l, a[i].r), swap(b, d);
        a[i].len = floor(sqrt((a[i].r-a[i].l)*(a[i].r-a[i].l) + (d-b)*(d-b)));
    }
    sort(a+1, a+1+n, [](seg x, seg y) {
        if(x.l != y.l) return x.l < y.l;
        return x.r < y.r;
    });

    for(int i = 1; i <= n; i++) {
        add(ss, i, 1, 0);
        add(i, i+n, 1, -a[i].len);
        add(i+n, t, 1, 0);
        for(int j = i+1; j <= n; j++) {
            if(a[j].l >= a[i].r)
                add(i+n, j, 1, 0);
        }
    }

    spfa();
    while(dijkstra()) {
        ll minf = inf;
        for(int i = 1; i <= t; i++) h[i] += dis[i];
        for(int i = t; i != s; i = p[i].to) minf = min(minf, e[p[i].e].w);
        for(int i = t; i != s; i = p[i].to) {
            e[p[i].e].w -= minf;
            e[p[i].e^1].w += minf;
        }
        minc += minf*h[t];
    }
    printf("%lld\n", -minc);
    return 0;
}
2023/8/13 10:30
加载中...