二分+2-sat

枚举第一个权值,二分第二个权值,然后2-sat检查,当第一个权值已经不能形成二分图时,再往下没意义,因为没法分成两个点集。(双指针好像跑得慢)

#include<bits/stdc++.h>
using namespace std;
const int N = ;
struct edge {
int u, v, w;
edge(int u = , int v = , int w = ) : u(u), v(v), w(w) {}
bool friend operator < (edge A, edge B)
{
return A.w > B.w;
}
} e[N * N];
int n, tot, cnt, all, top, ans;
int dfn[N], low[N], st[N], belong[N], vis[N], Map[N][N], color[N], fa[N], d[N];
vector<int> G[N], g[N];
int find(int x)
{
if(fa[x] == x) return x;
int ret = find(fa[x]);
d[x] ^= d[fa[x]];
return fa[x] = ret;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++cnt;
st[++top] = u;
vis[u] = ;
for(int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
if(!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(vis[v]) low[u] = min(low[u], low[v]);
}
if(low[u] == dfn[u])
{
++all;
int j;
while()
{
j = st[top];
belong[j] = all;
vis[j] = ;
--top;
if(j == u) break;
}
}
}
bool check(int x, int y)
{
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(vis, , sizeof(vis));
top = ;
cnt = ;
all = ;
if(x > y) swap(x, y);
for(int i = ; i <= * n; ++i)
G[i].clear();
for(int i = ; i <= tot; ++i)
{
if(e[i].w > y)
{
G[e[i].u].push_back(e[i].v + n);
G[e[i].v].push_back(e[i].u + n);
G[e[i].u + n].push_back(e[i].v);
G[e[i].v + n].push_back(e[i].u);
}
else if(e[i].w > x)
{
G[e[i].u].push_back(e[i].v + n);
G[e[i].v].push_back(e[i].u + n);
}
}
for(int i = ; i <= * n; ++i) if(!dfn[i]) tarjan(i);
for(int i = ; i <= n; ++i) if(belong[i] == belong[i + n]) return false;
return true;
}
int main()
{
scanf("%d", &n);
for(int i = ; i <= n; ++i) fa[i] = i;
for(int i = ; i < n; ++i)
for(int j = ; j <= n - i; ++j)
{
scanf("%d", &Map[i][i + j]);
e[++tot] = edge(i, i + j, Map[i][i + j]);
}
if(n == )
{
puts("");
return ;
}
++tot;
sort(e + , e + tot + );
ans = ;
int j = tot;
for(int i = ; i <= tot && i <= j; ++i)
{
while(j && !check(e[i].w, e[j].w)) --j;
int u = find(e[i].u), v = find(e[i].v);
if(u != v)
{
ans = min(ans, e[i].w + e[j].w);
d[u] = d[e[i].u] ^ d[e[i].v] ^ ;
fa[u] = v;
}
else if(d[e[i].u] == d[e[i].v])
{
ans = min(ans, e[i].w + e[j].w);
break;
}
}
printf("%d\n", ans);
return ;
}

bzoj4078的更多相关文章

  1. BZOJ4078 WF2014Metal Processing Plant(二分答案+2-SAT)

    题面甚至没给范围,由数据可得n<=200.容易想到二分答案,暴力枚举某集合的价值,2-SATcheck一下即可.这样是O(n4logn)的. 2-SAT复杂度已经是下界,考虑如何优化枚举.稍微改 ...

  2. BZOJ4078 : [Wf2014]Metal Processing Plant

    设$D(A)\leq D(B)$,从小到大枚举$D(A)$,双指针从大到小枚举$D(B)$. 那么对于权值不超过$D(A)$的边,可以忽略. 对于权值介于$(D(A),D(B)]$之间的边,需要满足那 ...

随机推荐

  1. pptp服务故障

    pptp服务故障记录 原文地址:http://www.cnblogs.com/caoguo/p/4994512.html 1.pptp部署是遇到开了防火墙端口不能拨号,拨号是提示错误如下: 但是关闭防 ...

  2. linux修改hosts配置

    参考 https://blog.csdn.net/qq_15192373/article/details/81093542 1. terminal中输入: sudo gedit /etc/hosts ...

  3. std::vector遍历

    std::vector是我在标准库中实用最频繁的容器.总结一下在遍历和创建vector时需要注意的一些地方. 在不考虑线程安全问题的前提下,在C++11中有五种遍历方式. 方式一 for (size_ ...

  4. GFS分布式文件系统脚本

    #!/bin/bashfor i in $(fdisk -l | grep -wo "/dev/sd[b-z]" | sort)dodd if=/dev/zero of=$i bs ...

  5. 11.11如何卖到一个亿:从0到1的电商爆品打造术 电子书 PDF

    内容转自:https://download.csdn.net/download/chenyao1994/11191034 下载地址:https://pan.baidu.com/s/1uQ1cjm9QH ...

  6. POJ1161——The Suspects

    POJ1161——The Suspects   The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 48 ...

  7. PAT 1106 Lowest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  8. BZOJ 4044 Luogu P4762 [CERC2014]Virus Synthesis (回文自动机、DP)

    好难啊..根本不会做..基本上是抄Claris... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4044 (luogu) ...

  9. RSA算法求明文

    #注:gmpy2 的安装请参考 http://www.cnblogs.com/gwind/p/8000570.html# -*- coding: utf-8 -*- import gmpy2 prin ...

  10. 华为/H3C Syslog配置

    H3C交换机的设置举例1. 组网需求将系统的日志信息发送到 linux 日志主机:日志主机的IP 地址为1.2.0.1/16:信息级别高于等于 informational 的日志信息将会发送到日志主机 ...