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

 题意:给出一个网络连通图n个服务器m条网线以及费用,现在需要控制某些网线,令1发出的信号无论如何都不能到达n,且保证选择的网线总费用与网线总条数的比值最小,问需要选择的网线条数,并给出它们的序号;

分析:设比值r=c/k=sigma(wi*xi)/sigma(xi),设最优值为R,即r>=R
即:sigma(wi*xi)/sigma(xi)>=R
即:sigma(wi-R)*xi>=0,所以要二分枚举R值,若wi-R的权值小于0,则一定要选择,并且选择最小的割集中的边,当存在某个R使得sigma(wi-R)*xi==0时,R就是最优解
然后从1dfs一遍,把容量不为零的边走一遍,并把所有的点标记,最后搜索一遍编号,最后的答案就是权值为负值的边和割集中的边
程序:
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"iostream"
#include"queue"
#include"stack"
#include"map"
#include"string"
#define M 409
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
struct node
{
int u,v,next;
double w;
}edge[M*10];
int t,head[M],work[M],a[M],b[M],c[M],dis[M],belong[M],use[M];
double min(double a,double b)
{
return a<b?a:b;
}
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,double w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
int bfs(int S,int T)
{
queue<int>q;
memset(dis,-1,sizeof(dis));
dis[S]=0;
q.push(S);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w>eps&&dis[v]==-1)
{
dis[v]=dis[u]+1;
q.push(v);
if(v==T)
return 1;
}
}
}
return 0;
}
double dfs(int cur,double a,int T)
{
if(cur==T)return a;
for(int &i=work[cur];~i;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w>eps&&dis[v]==dis[cur]+1)
{
double tt=dfs(v,min(a,edge[i].w),T);
if(tt)
{
edge[i].w-=tt;
edge[i^1].w+=tt;
return tt;
}
}
}
return 0;
}
double Dinic(int S,int T)
{
double ans=0;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
while(double tt=dfs(S,inf,T))
ans+=tt;
}
return ans;
}
double fun(int n,int m,double r)
{
init();
double sum=0;
for(int i=1;i<=m;i++)
{
if(c[i]>r)
{
add(a[i],b[i],c[i]-r);
add(b[i],a[i],c[i]-r);
}
else
sum+=c[i]-r;
}
return sum+Dinic(1,n);
}
void DFS(int u)
{
use[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w>eps&&!use[v])
DFS(v);
}
}
int main()
{
int n,m,kk=0;
while(scanf("%d%d",&n,&m)!=-1)
{
double l=0,r=0,mid;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
r+=c[i];
}
while(r-l>eps)
{
mid=(l+r)/2;
double msg=fun(n,m,mid);
if(msg>eps)
{
l=mid;
}
else
r=mid;
}
fun(n,m,mid);//重新跑一遍网络流,因为最后一次的网络流不一定是最优值mid的网络流
memset(use,0,sizeof(use));
DFS(1);
int num=0;
for(int i=1;i<=m;i++)
{
if(use[a[i]]!=use[b[i]]||c[i]<mid)
belong[num++]=i;
}
printf("%d\n",num);
printf("%d",belong[0]);
for(int i=1;i<num;i++)
printf(" %d",belong[i]);
printf("\n");
if(kk)
printf("\n");
kk++;
}
return 0;
}

  

01分数规划zoj2676(最优比例,最小割集+二分)的更多相关文章

  1. POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题

    http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...

  2. POJ 2728 Desert King 01分数规划,最优比率生成树

    一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...

  3. 【转】[Algorithm]01分数规划

    因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...

  4. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  5. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}

    P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...

  6. POJ3757 01分数规划

    题意:      有一个任务,给你提供n太服务器,让你在这n太服务器中选出k台完成这个任务,要求是每台服务器的工作时间相同,总的花费最小. 思路:      题目中给出对于每台服务器有这个式子: To ...

  7. 【BZOJ】4753: [Jsoi2016]最佳团体 01分数规划+树上背包

    [题意]n个人,每个人有价值ai和代价bi和一个依赖对象ri<i,选择 i 时 ri 也必须选择(ri=0时不依赖),求选择k个人使得Σai/Σbi最大.n<=2500,ai,bi< ...

  8. codevs1183 泥泞的道路(01分数规划)

    1183 泥泞的道路  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description CS有n个小区,并且任意小区之间都有两 ...

  9. zoj 2676 二分+ISAP模板求实型参数的最小割(0-1分数规划问题)(可做ISAP模板)

    /* 参考博文:http://www.cnblogs.com/ylfdrib/archive/2010/09/01/1814478.html 以下题解为转载代码自己写的: zoj2676 胡伯涛论文& ...

随机推荐

  1. Oracle存储过程java 调用

    1.nest表组合成结果集,然后以游标变量的形式返回 --创建类型 create or replace package mytest is -- Author  : ADMINISTRATOR  -- ...

  2. Demonstrating One-Shot Execution TimerTask Timer

    Listing -. Demonstrating One-Shot Execution import java.util.Timer; import java.util.TimerTask; publ ...

  3. document.execCommand(”BackgroundImageCache”, false, true)

    很多时候我们要给一些按钮或是img设置背景,而为了达到数据与表现样式分离的效果,通常背景样式都是在CSS里设定的,但是这个行为在IE会有一 个Bug,那就是因为 IE默认情况下不缓存背景图片,所以当鼠 ...

  4. VB动态添加WebBrowser控件,并拦截弹出窗口(不用引用任何组件)

    新建空白窗体,然后粘帖下面代码: Option ExplicitPublic WithEvents br As VBControlExtender Private Sub br_ObjectEvent ...

  5. sqlserver 简单的事物用法

    SELECT * FROM Interface_UserPort BEGIN TRY BEGIN TRAN Tran_2012_12_25 ,) --raiserror 50005N'抛出错误' CO ...

  6. 分布式中,zookeeper的部署

    一:准备 1.概述 为分布式应用提供协调服务的项目 类似于文件系统那样的树形数据结构 目的:将分布式服务不再由于协作冲突而另外实现协作服务 2.数据结构 树形数据结构 zookeeper的每个节点都是 ...

  7. 美国VPS - DigitalOcean 推荐创业团队使用

    初创公司DigitalOcean在美国正迅速成为一个家喻户晓的公司.每月5美元,该公司就可以让你享受到一个虚拟的私有服务器(或者说droplets,很多公司都这么称呼它).该公司的联合创始人兼首席执行 ...

  8. 使用C语言把字母转换成大写,不能使用库函数

    char to_upper(char input) {   if ('a' <= input && input <= 'z') {     return input - ' ...

  9. iOS 开发者账号共用发布证书 (Distribution)问题

    苹果客服回复: 1.第一台申请发布证书的电脑,从钥匙串中导出发布证书(Distribution)颁发的request文件?然后在第二台电脑上用request文件新生成一个Distribution证书, ...

  10. 北大,awk 命令基础练习

    北大,awk 命令基础练习 Red Hat Enterprise Linux Version 5.3 Get the latest news about the world's Open Source ...