ZOJ 2676 Network Wars[01分数规划]

ZOJ Problem Set - 2676

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

Input Output
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 

Source: Andrew Stankevich‘s Contest #8
Submit    Status

阿尔分红

卷三公分iiuaghsf

iugafi

include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef double real;
const real eps=1e-6;
const int Z=1e3+5,N=1e4+5,M=1e5+5;
struct data{int u,v,w;}a[Z];
struct edge{int v,next;real cap;}e[M];int tot=1,head[N];
int n,m,S,T,cas,num,g[Z],dis[N],q[M];bool vis[Z];
real L,R,ans;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline int dcmp(real x){
    if(fabs(x)<eps) return 0;
    return x>0?1:-1;
}
inline void add(int x,int y,real z){
    e[++tot].v=y;e[tot].cap=z;e[tot].next=head[x];head[x]=tot;
    e[++tot].v=x;e[tot].cap=z;e[tot].next=head[y];head[y]=tot;
}
inline bool bfs(){
    for(int i=S;i<=T;i++) dis[i]=-1;
    int h=0,t=1;q[t]=S;dis[S]=0;
    while(h!=t){
        int x=q[++h];
        for(int i=head[x];i;i=e[i].next){
            if(dcmp(e[i].cap)>0&&dis[e[i].v]==-1){
                dis[e[i].v]=dis[x]+1;
                if(e[i].v==T) return 1;
                q[++t]=e[i].v;
            }
        }
    }
    return 0;
}
real dfs(int x,real f){
    if(x==T) return f;
    real used=0,t;
    for(int i=head[x];i;i=e[i].next){
        if(dcmp(e[i].cap)>0&&dis[e[i].v]==dis[x]+1){
            t=dfs(e[i].v,min(e[i].cap,f));
            e[i].cap-=t;e[i^1].cap+=t;
            used+=t;f-=t;
            if(!f) return used;
        }
    }
    if(!used) dis[x]=-1;
    return used;
}
inline real dinic(){
    real res=0;
    while(bfs()) res+=dfs(S,2e9);
    return res;
}
inline void init(){
    m=read();L=0;R=0;num=0;S=1;T=n;
    for(int i=1;i<=m;i++) a[i].u=read(),a[i].v=read(),a[i].w=read(),R+=a[i].w;
}
inline real rebuild(real s){
    tot=1;memset(head,0,n+1<<2);
    real tans=0;
    for(int i=1;i<=m;i++){
        if(a[i].w>s){
            add(a[i].u,a[i].v,a[i].w-s);
        }
        else tans+=a[i].w-s;
    }
    return tans+dinic();
}
inline real binary_search(){
    real mid,now;
    while(dcmp(R-L)>0){
        mid=(L+R)/2;
        now=rebuild(mid);
        if(dcmp(now)>0) L=mid;
        else R=mid;
    }
    return mid;
}
void DFS(int x){
    vis[x]=1;
    for(int i=head[x];i;i=e[i].next){
        if(!vis[e[i].v]&&dcmp(e[i].cap)>0){
            DFS(e[i].v);
        }
    }
}
inline void work(){
    ans=binary_search();
    memset(vis,0,n+1<<2);
    //rebuild(ans);
    DFS(S);
    for(int i=1;i<=m;i++){
        if(vis[a[i].u]+vis[a[i].v]==1||a[i].w<ans){
            g[++num]=i;
        }
    }
    if(cas++) putchar(‘\n‘);
    printf("%d\n",num);
    for(int i=1;i<num;i++) printf("%d ",g[i]);
    if(num) printf("%d",g[num]);
    putchar(‘\n‘);
}
int main(){
    while(~scanf("%d",&n)) init(),work();
    return 0;
}
时间: 2024-12-21 01:21:36

ZOJ 2676 Network Wars[01分数规划]的相关文章

HDU 2676 Network Wars 01分数规划,最小割 难度:4

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 对顶点i,j,起点s=1,终点t=n,可以认为题意要求一组01矩阵use[i][j],使得aveCost=sigma(use[i][j]*cost[i][j])/sigma(use[i][j])最小,且{(i,j)|use[i][j]==1}是图的S-T割 定义F(e)=min(sigma(use[i][j]*(cost[i][j]-a))),明显,F(e)是目标式的变

zoj2676 Network Wars(0-1分数规划,最大流模板)

Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include <algorithm> #include <cstdio> #include <iterator> #include <limits> #include <vector> #include <string.h> const int N = 11

zoj 2676 Network Wars(最小割,01分数规划)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2676 大致题意:给出一个带权无向图,每条边有一个边权wi,求将S和T分开的一个割边集C,使得该割边集的平均边权最小,即最小化∑wi / |C| . 详见amber关于最小割模型的论文 思路:amber论文中详细讲解了如何转化成函数及建图,值得注意的是当边被重新赋权后,对于wi < 0 的边权,该边必然在最小割中,不必再建边,直接加入最大流中即可,因为求最小割时边权都为正值

zoj 2676 Network Wars 最小割+0-1分数规划

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2676 题意: 给出N个点和M条边,要求一个割集,使得集合中 ans = 所有边点权值和/边的数量 最小. 思路: 0-1分数规划. 具体证明参考 胡伯涛 <最小割模型在信息学竞赛中的应用> 设g = min(Σwi/Σ1) 转化为 求g使得 (Σwi - g*Σ1) = 0. 所以二分求g的值. 每次建图的时候,以1为源点,N为汇点. 原来图中每条边的容量

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

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 n

zoj2676--Network Wars(0-1分数规划+最小割)

zoj2676:题目链接 题目大意:有一个n个点的网络,其中有m条光缆(所有的点都被连接,任意两个点之间最多有一条,不存在连接自身的),每条光缆有一定的价值,网络中1为起点,n为终点,现在要求找出一些光缆能分割开1到n,使它们不能相互通信,并且要求花费的和除以光缆数的值最小.输出选择的光缆的编号. 从问题中可以看出一定是0-1分数规划的题目,假设选出光缆的集合M,M为原图的一个割,光缆si∈M,价值为ci,数量k = 1 ,可以推出g(x) = min( ∑c - x*∑k ),因为si是割中的

01分数规划(转)

01分数规划 分类: DP&&记忆化搜索2013-05-04 14:47 4193人阅读 评论(1) 收藏 举报 [关键字] 0/1分数规划.最优比率生成树.最优比率环 [背景] 根据楼教主的回忆录,他曾经在某一场比赛中秒掉了一道最优比率生成树问题,导致很多人跟风失败,最终悲剧.可见最优比率生成树是多么凶残的东西,但是这个东西只要好好研究半天就可以掌握,相信你在看了我写的这篇总结之后可以像楼教主一般秒掉这类问题. 因为网上对于01分数规划问题的详细资料并不是太多,所以我就结合自己的一些理解

【转】[Algorithm]01分数规划

因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html [关键字] 0/1分数规划.最优比率生成树.最优比率环 [背景] 根据楼教主的回忆录,他曾经在某一场比赛中秒掉了一道最优比率生成树问题,导致很多人跟风失败,最终悲剧. 自己总结了一些这种问题的解法,因为水平有限,如果有错误或是麻烦的地方,尽管喷,邮箱或是下方留言

01分数规划zoj2676(最优比例,最小割集+二分)

ZOJ Problem Set - 2676 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 direction