【ZOJ2676】Network Wars 最小割+01分数规划

Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both directions. Two servers of the network are especially important — they are connected to global world network and president palace network respectively.

The server connected to the president palace network has number 1, and the server connected to the global world network has number n.

Recently the company Max Traffic has decided to take control over some cables so that it could see what data is transmitted by the president palace users. Of course they want to control such set of cables, that it is impossible to download any data from the global network to the president palace without transmitting it over at least one of the cables from the set.

To put its plans into practice the company needs to buy corresponding cables from their current owners. Each cable has some cost. Since the company’s main business is not spying, but providing internet connection to home users, its management wants to make the operation a good investment. So it wants to buy such a set of cables, that cables mean cost} is minimal possible.

That is, if the company buys k cables of the total cost c, it wants to minimize the value of c/k.

Input

There are several test cases in the input. The first line of each case contains n and m (2 <= n <= 100 , 1 <= m <= 400 ). Next m lines describe cables~— each cable is described with three integer numbers: servers it connects and the cost of the cable. Cost of each cable is positive and does not exceed 107.
Any two servers are connected by at most one cable. No cable connects a server to itself. The network is guaranteed to be connected, it is possible to transmit data from any server to any other one.

There is an empty line between each cases.

Output

First output k — the number of cables to buy. After that output the cables to buy themselves. Cables are numbered starting from one in order they are given in the input file. There should an empty line between each cases.

Example

Input Output
6 8 4
1 2 3 3 4 5 6
1 3 3
2 4 2
2 5 2
3 4 2
3 5 2
5 6 3
4 6 3
4 5 3
1 2 2 1 2 3
1 3 2
2 3 1
2 4 2
3 4 2

题意:给个无向图,源点为1汇点为n,求一个流量平均数最小的割,即找一个割使 eCfe/|C| 最小。输出割上边的编号和割的大小。

平均数,01分数规划:二分平均数大小然后让流量通通减去mid,然后求最小割。若流量-mid小于0则直接选这条边,否则建图跑最小割。

01分数规划还得复习一下啊…

关于如何输出割:

割边一定满流,满流却不一定是割边。残量网络上从源点dfs,把能扫到的点打上标记,若一条边同时连着有标记和无标记的点,则这个边是割边。

话说vjudge又submitfailed…

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

const double INF = 1000000010;
const int SZ = 1000010;

int head[SZ],nxt[SZ],tot = 1,n,m;

struct edge{
    int t;
    double d;
}l[SZ];

void build(int f,int t,double d)
{
    l[++ tot].t = t;
    l[tot].d = d;
    nxt[tot] = head[f];
    head[f] = tot;
}

void insert(int f,int t,double d)
{
    build(f,t,d); build(t,f,0);
}

int deep[SZ];
queue<int> q;

bool bfs(int s,int e)
{
    memset(deep,0,sizeof(deep));
    deep[s] = 1;
    while(q.size()) q.pop();
    q.push(s);
    while(q.size())
    {
        int u = q.front(); q.pop();
// printf("%d\n",u);
        for(int i = head[u];i;i = nxt[i])
        {
            int v = l[i].t;
            if(!deep[v] && l[i].d)
            {
                deep[v] = deep[u] + 1;
                q.push(v);
                if(v == e) return true;
            }
        }
    }
    return false;
}

double dfs(int u,double flow,int e)
{
    if(u == e || flow == 0) return flow;
    double rest = flow;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(deep[v] == deep[u] + 1 && l[i].d)
        {
            double f = dfs(v,min(rest,l[i].d),e);
            if(f > 0)
            {
                l[i].d -= f;
                l[i ^ 1].d += f;
                rest -= f;
                if(rest == 0) break;
            }
            else deep[v] = 0;
        }
    }
    return flow - rest;
}



double dinic(int s,int e)
{
    double ans = 0;
    while(bfs(s,e)) ans += dfs(s,INF,e);
    return ans;
}

void init()
{
    tot = 1;
    memset(head,0,sizeof(head));
}

int ff[SZ],tt[SZ],cc[SZ];

double check(double mid)
{
    init();
    double ans = 0;
    for(int i = 1;i <= m;i ++)
        if(cc[i] - mid <= 0)
            ans += cc[i] - mid;
        else
            insert(ff[i],tt[i],cc[i] - mid);
    double tmp = dinic(1,n);
// printf("%lf %lf\n",mid,tmp);
    ans += tmp;
// printf("%lf %lf\n",mid,ans);
    return ans;
}

double div()
{
    double l = -1,r = INF;
    for(int i = 1;i <= 50;i ++)
    {
        double mid = (l + r) / 2;
        if(check(mid) > 1e-8) l = mid;
        else r = mid; 
    }
    return r;
}

bool vis[SZ];

void dfs(int u)
{
    vis[u] = 1;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(l[i].d && !vis[v])
            dfs(v);
    }
}

int lst[SZ];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i ++)
        scanf("%d%d%d",&ff[i],&tt[i],&cc[i]);

    double ans = div();

    check(ans);

    dfs(1);

    for(int i = 1;i <= m;i ++)
    {
        if(cc[i] - ans <= 1e-8 || vis[ff[i]] + vis[tt[i]] == 1)
            lst[++ lst[0]] = i;
    }
    printf("%d\n",lst[0]);
    for(int i = 1;i <= lst[0];i ++)
        printf("%d ",lst[i]);   

    return 0;
}

你可能感兴趣的:(【ZOJ2676】Network Wars 最小割+01分数规划)