POJ 3155 最大密度子图 二分+最小割

还是参考amber的论文 最小割那篇


这道题建图的话按照论文上建就可以。

但是对于本题来讲,最最蛋疼的地方绝对不是建图,而是精度。

比方说最后遍历残留网络的时候,因为是double类型么,我理所当然的用了eps去判断与0的关系,然后就杯具了。。。 交了十几次, 赫然发现尼玛直接写>0远比eps好用。

然后二分的部分也需要用到eps的时候,比如high与low的差大于eps,h(g)的值与eps比较,这样的eps从1e-2到1e-10均好使,什么,你想精度再高点?不好意思,再高就wa了

当然, high-low这一部分的eps可以用1.0/n/n来替代,这是一个定理 

注意二分完了,一定要再用low建图求一遍最大流 否则还是wa 据说这是因为这个函数的图形非常奇特,经常会来个突然的变化


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 111
#define MAXM 11111
#define INF 1000000007
using namespace std;
struct node
{
    int ver;    // vertex
    double cap;    // capacity
    double flow;   // current flow in this arc
    int next, rev;
}edge[MAXM];
int dist[MAXN], numbs[MAXN], src, des, n;
int head[MAXN], e;
void add(int x, int y, double c)
{       //e记录边的总数
    edge[e].ver = y;
    edge[e].cap = c;
    edge[e].flow = 0;
    edge[e].rev = e + 1;        //反向边在edge中的下标位置
    edge[e].next = head[x];   //记录以x为起点的上一条边在edge中的下标位置
    head[x] = e++;           //以x为起点的边的位置
    //反向边
    edge[e].ver = x;
    edge[e].cap = 0;  //反向边的初始容量为0
    edge[e].flow = 0;
    edge[e].rev = e - 1;
    edge[e].next = head[y];
    head[y] = e++;
}
void rev_BFS()
{
    int Q[MAXN], qhead = 0, qtail = 0;
    for(int i = 1; i <= n; ++i)
    {
        dist[i] = MAXN;
        numbs[i] = 0;
    }
    Q[qtail++] = des;
    dist[des] = 0;
    numbs[0] = 1;
    while(qhead != qtail)
    {
        int v = Q[qhead++];
        for(int i = head[v]; i != -1; i = edge[i].next)
        {
            if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue;
            dist[edge[i].ver] = dist[v] + 1;
            ++numbs[dist[edge[i].ver]];
            Q[qtail++] = edge[i].ver;
        }
    }
}
void init()
{
    e = 0;
    memset(head, -1, sizeof(head));
}
double maxflow()
{
    int u;
    double totalflow = 0;
    int Curhead[MAXN], revpath[MAXN];
    for(int i = 1; i <= n; ++i)Curhead[i] = head[i];
    u = src;
    while(dist[src] < n)
    {
        if(u == des)     // find an augmenting path
        {
            double augflow = INF;
            for(int i = src; i != des; i = edge[Curhead[i]].ver)
                augflow = min(augflow, edge[Curhead[i]].cap);
            for(int i = src; i != des; i = edge[Curhead[i]].ver)
            {
                edge[Curhead[i]].cap -= augflow;
                edge[edge[Curhead[i]].rev].cap += augflow;
                edge[Curhead[i]].flow += augflow;
                edge[edge[Curhead[i]].rev].flow -= augflow;
            }
            totalflow += augflow;
            u = src;
        }
        int i;
        for(i = Curhead[u]; i != -1; i = edge[i].next)
            if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break;
        if(i != -1)     // find an admissible arc, then Advance
        {
            Curhead[u] = i;
            revpath[edge[i].ver] = edge[i].rev;
            u = edge[i].ver;
        }
        else        // no admissible arc, then relabel this vertex
        {
            if(0 == (--numbs[dist[u]]))break;    // GAP cut, Important!
            Curhead[u] = head[u];
            int mindist = n;
            for(int j = head[u]; j != -1; j = edge[j].next)
                if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]);
            dist[u] = mindist + 1;
            ++numbs[dist[u]];
            if(u != src)
                u = edge[revpath[u]].ver;    // Backtrack
        }
    }
    return totalflow;
}
int d[MAXN];
int xx[MAXM], yy[MAXM];
int nt, m;
int vis[MAXN], ans;
void dfs(int u)
{
    vis[u] = 1;
    if(u <= nt) ans++;
    for(int i = head[u]; i != -1; i = edge[i].next)
        if(!vis[edge[i].ver] && edge[i].cap > 0)
            dfs(edge[i].ver);
}
void build(double mid)
{
    for(int i = 1; i <= m; i++)
    {
        add(xx[i], yy[i], 1);
        add(yy[i], xx[i], 1);
    }
    for(int i = 1; i <= nt; i++)
    {
        add(src, i, m);
        add(i, des, m * 1.0 + 2 * mid - d[i] * 1.0);
    }
}
int main()
{
    while(scanf("%d%d", &nt, &m) != EOF)
    {
        if(m == 0)
        {
            printf("1\n1\n");
            continue;
        }
        memset(d, 0, sizeof(d));
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d", &xx[i], &yy[i]);
            d[xx[i]]++;
            d[yy[i]]++;
        }
        double low = 0, high = m;
        src = nt + 1;
        des = nt + 2;
        n = des;
        while(high - low > 1.0 / nt / nt)
        {
            init();
            double mid = (low + high) / 2;
            build(mid);
            rev_BFS();
            double h = (m * nt * 1.0 - maxflow()) / 2;
            if(h > eps) low = mid;
            else high = mid;
        }
        init();
        build(low);
        rev_BFS();
        maxflow();
        memset(vis, 0, sizeof(vis));
        ans = 0;
        dfs(src);
        printf("%d\n", ans);
        for(int i = 1; i <= nt; i++)
            if(vis[i]) printf("%d\n", i);
    }
    return 0;
}



你可能感兴趣的:(c,网络,Build,Path,图形)