Network Wars-ZOJ2676最小割+01規劃

Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge

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.node

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

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.markdown

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.ide

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

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.spa

There is an empty line between each cases.code

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.
Exampleserver

Input

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

3
1 2 3
Source: Andrew Stankevich’s Contest #8ci

在Amber寫的《最小割在信息學競賽中的應用》看到的一道例題,因此就拿來作作,可是出現了很多的問題
題意:給出一個帶權的無向圖,每一條邊有一個權值w,求將s與t分開的一個邊割集,使得邊割集的平均值最小。
具體的作法能夠看看AMber的論文,這裏有幾個疑惑
1. 爲何在DFS過程當中不加引用就TLE
2. 爲何在Dinic過程當中不復制就會WA

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>

using namespace std;

const double eps = 1e-6;

const int INF = 0x3f3f3f3f;

const int MaxN = 110;

const int MaxM = 51000;

typedef struct Node
{
    int u,v,cap;
}Point ;

typedef struct node
{
    int v,next;

    double cap;
}edge;

Point a[MaxN*4];

edge e[MaxM];

int H[MaxN],Head[MaxN],top,vis[MaxN];

int n,m;

double L,R;

int dbcmp(double s)
{
    if(fabs(s)<eps)
    {
        return 0;
    }

    return s>0?1:-1;
}

void AddEdge(int u,int v,double cap)
{
    e[top].v =v ;e[top].cap = cap;

    e[top].next = H[u]; H[u] = top++;
}

bool BFS()
{
    memset(vis,0,sizeof(vis));

    vis[1] =1;

    queue<int>Q;

    Q.push(1);

    while(!Q.empty())
    {
        int  u =Q.front();

        Q.pop();

        for(int i = H[u];~i;i = e[i].next)
        {
            if(dbcmp(e[i].cap)>0&&!vis[e[i].v])
            {
                vis[e[i].v] = vis[u]+1;

                Q.push(e[i].v);

                if(e[i].v==n)
                {
                    return 1;
                }
            }
        }
    }

    return 0;
}

double DFS(int u,double cap)
{
    if(u==n)
    {
        return cap;
    }
    double ans =0;

    for(int &i =Head[u];i!=-1; i = e[i].next) //不加引用就超時
    {
        if(vis[e[i].v]==vis[u]+1&&dbcmp(e[i].cap)>0)
        {
            double ant = DFS(e[i].v,min(cap,e[i].cap));

            if(ant)
            {
                e[i].cap-=ant;

                e[i^1].cap+=ant;

                return ant;
            }
        }
    }
    return 0;
}

double Dinic()//求最小割
{
    double ans = 0;

    while(BFS())
    {
        memcpy(Head,H,sizeof(H));//不復制就WA
        while(double ant = DFS(1,INF))
            ans+=ant;
    }
    return ans;
}

double Build(double s)
{
    top =0;

    memset(H,-1,sizeof(H));

    double ans = 0;

    for(int i=1;i<=m;i++)
    {
        if(a[i].cap>s)
        {
            AddEdge(a[i].u,a[i].v,a[i].cap-s);

            AddEdge(a[i].v,a[i].u,a[i].cap-s);
        }
        else ans += (a[i].cap-s);
    }

    return ans+Dinic();
}

double Search()
{
    double mid;

    while(dbcmp(R-L)>0)
    {
        mid = (L+R)/2;

        double ant = Build(mid);

        if(dbcmp(ant)>0)
        {

            L = mid;
        }
        else
        {
            R = mid;
        }
    }
    return mid;
}

void dfs(int u)
{
    vis[u] = 1;

    for(int i = H[u];i!=-1;i = e[i].next)
    {
        if(dbcmp(e[i].cap)>0&&!vis[e[i].v])
        {
            dfs(e[i].v);
        }
    }
}

int main()
{
    int z = 0;

    while(~scanf("%d %d",&n,&m))
    {
        L = 0,R = 0;
        for(int i=1;i<=m;i++) 
        {
            scanf("%d %d %d",&a[i].u,&a[i].v,&a[i].cap);

            R+=a[i].cap;
        }

        double ans = Search();

        Build(ans);

        memset(vis,0,sizeof(vis));

        dfs(1);

        int num = 0;

        for(int i=1;i<=m;i++)
        {
            if(vis[a[i].v]+vis[a[i].u]==1||a[i].cap<ans)
            {
                num++;
            }
        }

        if(z++)
        {
            printf("\n");
        }

        printf("%d\n",num);

        bool flag = false;

        for(int i=1;i<=m;i++)
        {
            if(vis[a[i].v]+vis[a[i].u]==1||a[i].cap<ans)
            {
                if(flag)
                {
                    printf(" ");
                }
                else flag= true;

                printf("%d",i);
            }
        }
        printf("\n");
    }
    return 0;
}
相關文章
相關標籤/搜索