0、题目大意:求两点之间的最小割之和

1、分析:很明显,最小割树,我们发现这个题并不能用n^3的方法来求答案。。

所以我们记录下所有的边,然后把边从大到小排序,然后跑一边类似kruskal的东西,顺便统计答案

TAT,这个题我被卡常数了,贴上TLE的代码吧。。。

#include <queue>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define inf 214748364

struct Edge{
    int from, to, cap, flow, next;
};
int head[3010], cur[3010];
Edge G[20010];
int tot;
int d[3010];
bool vis[3010];
int s, t, n, m;
int a[3010];
int b[3010];

inline void init(){
    memset(head, -1, sizeof(head));
    tot = -1;
    return;
}

inline void insert(int from, int to, int cap){
    G[++ tot] = (Edge){from, to, cap, 0, head[from]};
    head[from] = tot;
    G[++ tot] = (Edge){to, from, 0, 0, head[to]};
    head[to] = tot;
    return;
}

inline bool BFS(){
    for(int i = 1; i <= n; i ++) vis[i] = 0;
    queue<int> Q;
    Q.push(s);
    vis[s]=1;
    d[s]=0;
    while(!Q.empty()){
        int x = Q.front(); Q.pop();
        for(int i = head[x]; i != -1; i = G[i].next){
            Edge& e = G[i];
            if(e.cap - e.flow > 0 && !vis[e.to]){
                vis[e.to] = 1;
                d[e.to]=d[x]+1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}

inline int dfs(int x, int a){
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int& i = cur[x]; i != -1; i = G[i].next){
        Edge& e = G[i];
        if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, a))) > 0){
            e.flow += f;
            G[i ^ 1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}

inline int maxflow(){
    int res = 0;
    while(BFS()){
        for(int i = 1; i <= n; i ++) cur[i] = head[i];
        res += dfs(s, inf);
    }
    return res;
}

inline void Clear(){
    for(int i = 0; i <= tot; i += 2){
        G[i].flow = G[i ^ 1].flow = (G[i].flow + G[i ^ 1].flow) / 2;
    }
}

struct node{
    int u, v, w;
    inline bool operator < (const node& rhs) const{
        return w > rhs.w;
    }
} T[20010];
int num;

inline void add(int u, int v, int w){
    T[++ num].u = u;
    T[num].v = v;
    T[num].w = w;
}

inline void solve(int l, int r){
    if(l == r) return;
    int rl = rand() % (r - l + 1) + l;
    int rr = rand() % (r - l + 1) + l;
    if(rl == rr) rl ++;
    if(rl > r) rl -= 2;
    s = a[rl], t = a[rr];
    Clear();

    int tw = maxflow();
    //puts("fuck");
    add(a[rl], a[rr], tw);
    int L = l, R = r;
    for(int i = l; i <= r; i ++){
        if(vis[a[i]]) b[L ++] = a[i];
        else b[R --] = a[i];
    }
    for(int i = l; i <= r; i ++) a[i] = b[i];
    solve(l, L - 1); solve(L, r);
}

int ff[20010], size[20010];

inline int find(int x){
    return ff[x] == x ? x : ff[x] = find(ff[x]);
}

int main(){
   // srand(time(NULL));
    scanf("%d%d", &n, &m);
    init();
    for(int i = 1; i <= m; i ++){
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v, 1); insert(v, u, 1);
    }
    for(int i = 1; i <= n; i ++) a[i] = i;
    solve(1, n);
    LL ret = 0;
    sort(T + 1, T + num + 1);
    for(int i = 1; i <= n; i ++) size[i] = 1, ff[i] = i;
   // puts("fuck");
    for(int i = 1; i <= num; i ++){
        int tx = find(T[i].u), ty = find(T[i].v);
        if(size[tx] < size[ty]) swap(tx, ty);
        ret += (LL)T[i].w * size[tx] * size[ty];
        ff[ty] = tx;
        size[tx] += size[ty];
    }
    printf("%lld\n", ret);
    return 0;
}


BZOJ4435——[Cerc2015]Juice Junctions的更多相关文章

  1. BZOJ4435 : [Cerc2015]Juice Junctions

    最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...

  2. bzoj4435: [Cerc2015]Juice Junctions(最小割树+hash)

    传送门 首先最大流等于最小割,那么可以转化为最小割树来做(不知道什么是最小割树的可以看看这题->这里) 具体的做法似乎是$hash[i][j]$表示最小割为$i$时点$j$是否与$S$连通 然后 ...

  3. 【BZOJ4435】[Cerc2015]Juice Junctions Tarjan+hash

    [BZOJ4435][Cerc2015]Juice Junctions Description 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都 ...

  4. 【BZOJ-4435】Juice Junctions 最小割树(分治+最小割)+Hash

    4435: [Cerc2015]Juice Junctions Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 20  Solved: 11[Submi ...

  5. BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash

    分治最小割的题目,要求n2. 之前用的n3的方法自然不能用了. 于是用hash,设hash[i][j]表示在最小割为i的时候,j是否与S联通. 看懂这个需要理解一下最小割树的构造. 这种题建议用EK写 ...

  6. [CERC2015]Juice Junctions(边双连通+字符串hash)

    做法 考虑边数限制的特殊条件,显然答案仅有\(\{0,1,2,3\}\) 0:不联通 1:连通 2:边双连通 3:任意删掉一条边都为边双连通 考虑每次删边后记录各点的边双染色情况来特判\(3\):是否 ...

  7. Juice Junctions

    Juice Junctions 题目描述 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都是11升每秒.管道可能连接节点,每个节点最多可以连接3 ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. Gym - 101480 CERC 15:部分题目题解(队内第N次训练)

    -------------------题目难度较难,但挺有营养的.慢慢补. A .ASCII Addition pro:用一定的形式表示1到9,让你计算加法. sol:模拟. solved by fz ...

随机推荐

  1. 实现BPEL4WS演示:教程

    http://www.ibm.com/developerworks/cn/education/webservices/ws-bpelws/bpel_tutorial_cn.html 开始 什么是Bus ...

  2. 【原】gulp快速入门

    今天刚入职了一家新公司,结果明天就要开始项目了.上级说要用gulp来打包代码,所以今晚花了一晚来看这个gulp, 可以说已经入门了.所以做一个小小的总结 : 首先全局安装gulp npm instal ...

  3. js006-面向对象的程序设计

    js006-面向对象的程序设计 面向对象(Object-Oriented,OO)的语言有一个标志,那就是他们都有类的概念.而通过类可以创建多个具有相同属性和方法的对象. ECMA-262把对象定义为: ...

  4. sql 分页的两种写法

    string Strsql = string.Format(@"select ee.DOCUMENTNO,ee.APPLICANTNAME,ee.COMPANY,ee.REQUESTTIME ...

  5. apache无法正常启动,80端口被占用的解决方法

    apache无法正常启动,80端口被占用的解决方法 网上的方法: 仔细查看提示: make_sock: could not bind to address 0.0.0.0:80 恍然大悟,计算机上安装 ...

  6. Javascript中call、apply、bind函数

    javascript在函数创建的时候除了自己定义的参数外还会自动新增this和arguments两个参数 javascript中函数也是对象,call.apply.bind函数就是函数中的三个函数,这 ...

  7. 9月14日JavaScript循环语句作业解析

    1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 解法一: var gd = 8848; var cs = 0; while(true) { cs++; gd = gd ...

  8. Oracle数据库管理系统下对数据库操作常用命令

    desc表名;                                                                       /*查看表结构*/ alter table  ...

  9. Session的SqlServer模式的配置

    很多时候,由于各种莫名其妙的原因,会导致session丢失.不过ASP.NET还允许将会话数据存储到一个数据库服务器中,方法是将mode属性变成SqlServer. 在这种情况下,ASP.NET尝试将 ...

  10. Unity 下载

    Unity历史版本 http://wiki.ceeger.com/unity:history#unity_522f1 UNITY 下载存档 http://unity3d.com/cn/get-unit ...