zoj 2676 Network Wars(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 numbern.

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 cablesmean cost} is minimal possible.

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

Input

There are several test cases in the input. The first line of each case containsn and m (2 <= n <= 100 , 1 <= m <= 400 ). Nextm 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

 

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

 

 

题意:给出一个双向网络,求点1到点n的一个最小割,满足(最小割/割边的数目)最小。

思路:01分数规划,按边权-二分的值*1建边,若边权-二分的值*1小于0, 那么这条边是不需要建的,因为这条边肯定是割边。最后dfs找割边即可。

 

AC代码:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
using namespace std;

const double INF = 1e9;
const int maxn = 110;
const double eps = 1e-6;
struct Edge{
    int u, v;
    double cap, flow;
    int next, id;
}et[maxn * maxn];
int cnt[maxn], dis[maxn], pre[maxn], cur[maxn], eh[maxn], col[maxn], ans[500];
int x[500], y[500], z[500];
double low[maxn];
int n, m, s, t, num;
bool used[500];
void init(){
    memset(eh, -1, sizeof(eh));
    memset(used, false, sizeof(used));
    num = 0;
    s = 1, t = n;
}
void add(int u, int v, double cap, double flow, int id){
    Edge e = {u, v, cap, flow, eh[u], id};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, double cap, int id){
    add(u, v, cap, 0, id);
    add(v, u, 0, 0, id);
}
double isap(int s, int t, int nv){
    int u, v, now;
    double flow = 0;
    memset(low, 0, sizeof(low));
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
        if(et[now].cap - et[now].flow > eps && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(low[u], et[now].cap - et[now].flow);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow > eps&& dis[u] > dis[et[now].v] + 1)
            dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
double build(double d){
    init();
    double sum = 0;
    for(int i = 0; i < m; i++)
    {
        if(z[i] - d <= eps)
        {
            used[i + 1] = true;
            sum += z[i] - d;
        }
        else
        {
            addedge(x[i], y[i], z[i] - d, i + 1);
            addedge(y[i], x[i], z[i] - d, i + 1);
        }
    }
    return sum;
}
void dfs(int u){
    col[u] = 1;
    for(int i = eh[u]; i != -1; i = et[i].next)
    if(et[i].cap - et[i].flow > eps)
    {
        int v = et[i].v;
        if(!col[v]) dfs(v);
    }
}
void solve(){
    double low = 0, high = 10000001, mid, sum;
    while(high - low > eps)
    {
        mid = (high + low) / 2;
        sum = build(mid);
        sum += isap(s, t, t + 1);
        if(sum > eps) low = mid;
        else high = mid;
    }
    memset(col, 0, sizeof(col));
    dfs(s);
    for(int i = 0; i < num; i += 2)
    {
        int u = et[i].u, v = et[i].v;
        if(col[u] && !col[v]) used[et[i].id] = true;
    }
    int tot = 0;
    for(int i = 1; i <= m; i++)
    if(used[i]) ans[tot++] = i;
    printf("%d\n", tot);
    for(int i = 0; i < tot - 1; i++) printf("%d ", ans[i]);
    printf("%d\n", ans[tot - 1]);
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 0; i < m; i++)
        scanf("%d%d%d", &x[i], &y[i], &z[i]);
        solve();
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值