ZOJ 2676 Network Wars(最優比例最小割)

來源:互聯網
上載者:User

標籤:example   order   cond   sizeof   its   name   span   down   use   

Network WarsTime 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.

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

 

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 81 2 31 3 32 4 22 5 23 4 23 5 25 6 34 6 3
43 4 5 6 
4 51 2 21 3 22 3 12 4 23 4 2
31 2 3

 



 

題目連結:ZOJ 2676

此題叫我們求$\Sigma w_{ei} \over |E|$的最小值,其中所有的邊均在S-T的割中,可以發現當${\Sigma w_{ei} \over |E|}<r$時,存在$r‘={\Sigma w_{ei} \over |E|}$作為更優的r,那我們寫成$\Sigma w_{ei} - r*|E|<0$,存在一個左邊的結果使得等式成立,即找到左邊式子的最小值小於0即可,觀察左邊的式子,可以化簡成$\Sigma (w_{ei}-r)<0$,然後邊集e是一個割,又要求這個割集的最小值,那顯然就是求s-t的最小割即可,先用二分求出最佳的比例,然後在最後剩下的那個殘餘網路中找出割集。

代碼:

#include <bits/stdc++.h>using namespace std;#define INF 0x3f3f3f3f#define LC(x) (x<<1)#define RC(x) ((x<<1)+1)#define MID(x,y) ((x+y)>>1)#define CLR(arr,val) memset(arr,val,sizeof(arr))#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);typedef pair<int, int> pii;typedef long long LL;const double PI = acos(-1.0);const int N = 110;const int M = 410;const double eps = 1e-6;struct edge{    int to, nxt;    double cap;    edge() {}    edge(int _to, int _nxt, double _cap): to(_to), nxt(_nxt), cap(_cap) {}};struct Node{    int u, v;    double cap;};Node e[M];edge E[M << 1];int head[N], tot;int d[N];int use[M];void init(){    CLR(head, -1);    tot = 0;    CLR(use, 0);}inline void add(int s, int t, double cap){    E[tot] = edge(t, head[s], cap);    head[s] = tot++;    E[tot] = edge(s, head[t], cap);    head[t] = tot++;}int bfs(int s, int t){    CLR(d, -1);    d[s] = 0;    queue<int>Q;    Q.push(s);    while (!Q.empty())    {        int u = Q.front();        Q.pop();        for (int i = head[u]; ~i; i = E[i].nxt)        {            int v = E[i].to;            if (d[v] == -1 && E[i].cap > 0)            {                d[v] = d[u] + 1;                if (v == t)                    return 1;                Q.push(v);            }        }    }    return ~d[t];}double dfs(int s, int t, double f){    if (s == t || !f)        return f;    double ret = 0;    for (int i = head[s]; ~i; i = E[i].nxt)    {        int v = E[i].to;        if (d[v] == d[s] + 1 && E[i].cap > 0)        {            double df = dfs(v, t, min(f, E[i].cap));            if (df > 0)            {                E[i].cap -= df;                E[i ^ 1].cap += df;                f -= df;                ret += df;                if (!f)                    break;            }        }    }    if (!ret)        d[s] = -1;    return ret;}double dinic(int s, int t){    double ans = 0;    while (bfs(s, t))        ans += dfs(s, t, INF);    return ans;}double Mincut(int n, int m, double r){    int i;    init();    double ret = 0;    for (i = 1; i <= m; ++i)    {        if (e[i].cap < r)        {            ret += e[i].cap - r;            use[i] = 1;        }        else            add(e[i].u, e[i].v, e[i].cap - r);    }    return ret + dinic(1, n);}int main(void){    int n, m, i;    while (~scanf("%d%d", &n, &m))    {        for (i = 1; i <= m; ++i)            scanf("%d%d%lf", &e[i].u, &e[i].v, &e[i].cap);        double Rat = 0, L = 0, R = 400.0 / 3 * 1e7;        while (fabs(R - L) >= eps)        {            double mid = (L + R) / 2.0;            if (Mincut(n, m, mid) < 0)            {                R = mid;                Rat = mid;            }            else                L = mid;        }        vector<int>ans;        for (i = 1; i <= m; ++i)        {            if ((d[e[i].u]!=-1)^(d[e[i].v]!=-1))                use[i] = 1;            if (use[i])                ans.push_back(i);        }        int sz = ans.size();        printf("%d\n", sz);        for (i = 0; i < sz; ++i)            printf("%d%c", ans[i], " \n"[i == sz - 1]);    }    return 0;}

ZOJ 2676 Network Wars(最優比例最小割)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.