zoj 2676(最小割+01分数规划)

Network Wars

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.

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

InputOutput
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
3 4 5 6 
4 5
1 2 2
1 3 2
2 3 1
2 4 2
3 4 2
3
1 2 3


简直了,A了一天没过。。。最后都不知道自己在纠结啥。。。


题目大意:给一个无向图,每个边有一个权值,wi,要求求一个割集,使得sigma(w[i])/l最大。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>

#define INF 0x3f3f3f3f
#define eps 1e-6
#define MAXN 400

using namespace std;

int n, m;
int x[MAXN], y[MAXN];
double c[MAXN];
double ans;

struct edge { int to; double cap; int rev; };

vector<edge> G[MAXN];
int level[MAXN];
int iter[MAXN];
bool mark[MAXN];

void add_edge(int from, int to, double cap) {
    G[from].push_back((edge){to, cap, G[to].size()});
    G[to].push_back((edge){from, 0, G[from].size() - 1});
}

void bfs(int s) {
    memset(level, -1, sizeof level);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
        int v = que.front(); que.pop();
        for (int i = 0; i < G[v].size(); i++) {
            edge &e = G[v][i];
            if (e.cap > eps && level[e.to] < 0) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

double dfs(int v, int t, double f) {
    if(v == t) return f;
    for (int &i = iter[v]; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > eps && level[v] < level[e.to]) {
            double d = dfs(e.to, t, min(f, e.cap));
            if(d > eps) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

double max_flow(int s, int t) {
    double flow = 0;
    for(;;) {
        bfs(s);
        if (level[t] < 0) return flow;
        memset(iter, 0, sizeof iter);
        double f;
        while ((f = dfs(s, t, INF)) > eps) {
            flow += f;
        }
    }
}

void creategraph(double mid) {
    ans = 0;
    for (int i = 0; i <= n; i++)
        G[i].clear();
    for (int i = 0; i < m; i++) {
        if(c[i] - mid < eps) {
            ans += c[i] - mid;
        }
        else {
            add_edge(x[i], y[i], c[i] - mid);
            add_edge(y[i], x[i], c[i] - mid);
        }
    }
}

double Binary_search() {
    double l = 0, r = 1e9;
    while(r - l > eps) {
        double mid = (l + r) / 2;
        creategraph(mid);
        if(ans + max_flow(1, n) >= eps) l = mid;
        else r = mid;
    }
    return l;
}

void dfs2(int u) {
    for (int i = 0; i < G[u].size(); i++) {
        if(G[u][i].cap > eps && !mark[G[u][i].to]) {
            mark[G[u][i].to] = 1;
            dfs2(G[u][i].to);
        }
    }
}

void solve() {
    memset(mark, false, sizeof mark);
    double rate = Binary_search();
    mark[1] = 1;
    dfs2(1);
    int ans = 0;
    for (int i = 0; i < m; i++) {
        if(c[i] - rate < eps || mark[x[i]] + mark[y[i]] == 1)
            ans++;
    }
    printf("%d\n", ans);
    for (int i = 0; i < m; i++) {
        if(c[i] - rate < eps || mark[x[i]] + mark[y[i]] == 1) {
            if(ans != 1)
                printf("%d ", i + 1);
            else printf("%d\n", i + 1);
            ans--;
        }
    }

}

int main()
{
    while (~scanf("%d%d", &n, &m)) {
        for (int i = 0; i < m; i++) {
            scanf("%d%d%lf", &x[i], &y[i], &c[i]);
        }
        solve();
    }
    return 0;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值