BZOJ2960:跨平面

题面

BZOJ

Sol

对该平面图的对偶图建图后就是最小树形图,建一个超级点向每个点连 inf i n f 边即可

怎么转成对偶图,怎么弄出多边形
把边拆成两条有向边,分别挂在两个点上
每个点的出边按角度排序
每次选择一个没有标记过的边做 DFS D F S
u u v,然后 v v 选择 (v,u) 顺时针转的下一条边,最后跑到原来的点,此时一定有一个多边形形成,记录编号后标记在边上即可

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

const int maxn(100005);
const int inf(1e9);

namespace MST{
    int n, cnt, pre[maxn], vis[maxn], id[maxn], ans, inw[maxn], sum, rt;

    struct Edge{
        int u, v, w;
    } e[maxn];

    IL void Add(RG int u, RG int v, RG int w){
        e[++cnt] = (Edge){u, v, w};
    }

    IL int DirectedMST(){
        for(RG int i = 1; i <= cnt; ++i) sum += e[i].w;
        ans -= (++sum);
        for(RG int i = 1; i <= n; ++i) Add(n + 1, i, sum);
        rt = ++n;
        while(true){
            RG int idx = 0;
            for(RG int i = 1; i <= n; ++i) pre[i] = id[i] = vis[i] = -1, inw[i] = inf;
            for(RG int i = 1; i <= cnt; ++i)
                if(e[i].u != e[i].v && e[i].w < inw[e[i].v]) inw[e[i].v] = e[i].w, pre[e[i].v] = e[i].u;
            inw[rt] = 0, pre[rt] = rt;
            for(RG int i = 1; i <= n; ++i){
                ans += inw[i];
                if(vis[i] == -1){
                    RG int x = i;
                    while(vis[x] == -1) vis[x] = i, x = pre[x];
                    if(x != rt && vis[x] == i){
                        id[x] = ++idx;
                        for(RG int j = pre[x]; j != x; j = pre[j]) id[j] = idx;
                    }
                }
            }
            if(!idx) break;
            for(RG int i = 1; i <= n; ++i) if(id[i] == -1) id[i] = ++idx;
            for(RG int i = 1; i <= cnt; ++i)
                e[i].w -= inw[e[i].v], e[i].u = id[e[i].u], e[i].v = id[e[i].v];
            n = idx, rt = id[rt];
        }
        return ans;
    }
}

struct Line{
    int u, v, id, type;
    double k;

    IL int operator <(RG Line b) const{
        return k < b.k;
    }
} line[maxn];

struct Point{
    int x, y;
} a[maxn];

struct Edge{
    int u, v, next, type, id;
} edge[maxn];

int n, m, num, first[maxn], cnt, mat[2][maxn], vis[maxn], val[2][maxn];

IL void Add(RG int u, RG int v, RG int type, RG int id){
    edge[cnt] = (Edge){u, v, first[u], type, id}, first[u] = cnt++;
}

IL int Dfs(RG int u, RG int ff){
    if(vis[u]) return ++MST::n;
    for(RG int e = first[u]; e != -1; e = edge[e].next){
        RG int v = edge[e].v, id, type;
        if(v == ff){
            e = edge[e].next;
            if(e == -1) e = first[u];
            v = edge[e].v, id = edge[e].id, type = edge[e].type;
            return mat[type][id] = Dfs(v, u);
        }
    }
    return 0;
}

int main(){
    n = Input(), m = Input();
    for(RG int i = 1; i <= n; ++i) first[i] = -1;
    for(RG int i = 1; i <= n; ++i) a[i].x = Input(), a[i].y = Input();
    for(RG int i = 1, x, y; i <= m; ++i){
        x = Input(), y = Input(), val[0][i] = Input(), val[1][i] = Input();
        line[++num] = (Line){x, y, i, 0}, line[num].k = atan2(a[y].y - a[x].y, a[y].x - a[x].x);
        line[++num] = (Line){y, x, i, 1}, line[num].k = atan2(a[x].y - a[y].y, a[x].x - a[y].x);
    }
    sort(line + 1, line + num + 1);
    for(RG int i = 1; i <= num; ++i) Add(line[i].u, line[i].v, line[i].type, line[i].id);
    for(RG int i = 0; i < cnt; ++i)
        if(!mat[edge[i].type][edge[i].id]){
            vis[edge[i].u] = 1;
            mat[edge[i].type][edge[i].id] = Dfs(edge[i].v, edge[i].u);
            vis[edge[i].u] = 0;
        }
    for(RG int i = 1; i <= m; ++i){
        if(val[0][i]) MST::Add(mat[0][i], mat[1][i], val[0][i]);
        if(val[1][i]) MST::Add(mat[1][i], mat[0][i], val[1][i]);
    }
    printf("%d\n", MST::DirectedMST());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值