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

    #include<stdio.h> int map[6][4]={8,0,18,10,      13,10,15,20,      10,50,13,30,               ...

  2. Nginx 上的 php-fpm 资源侵占问题

    测试人员向我们反映:在Facebook平台的游戏比其它平台的游戏明显更慢.我询问,是不是因为FQ网络原因.他们说:不是,其它游戏也比较流畅.使用httpwatch查看了http请求,发现api.php ...

  3. bug

    expected identifier,string or number   //这种问题一般是json数据中最后一个逗号没去掉.

  4. linux ssh scp 命令

    ssh jackielee@192.168.1.103 scp jackielee@192.168.1.103:/home/jackielee/develop/helloworld helloworl ...

  5. Java高级之虚拟机加载机制

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.0版本:2016-05-21 SubClass!! 执行结果说明一个问题:子类调用父类变量的时候 ...

  6. 【android学习2】:Eclipse中HttpServlet类找不到

    Eclipse中使用的HttpServlet类之所以识别不到的原因是没有导入Servlet-api.jar包,这个包在所安装在的tomcat的lib文件下,所以只需要导入即可. 在需要导入的工程上右键 ...

  7. ubuntu下的jdk安装

    软件环境: 虚拟机:VMware Workstation 10 操作系统:ubuntu-12.04-desktop-amd64 JAVA版本:jdk-7u55-linux-x64 软件下载地址: JD ...

  8. oracle截取某个字符前面的字符串

    已验证. 要求:A.数据库表中的一个字符串 可能含有"+" 例:ORC+001 也可能不含“+” B.要求如果该字符串含有“+”,则取“+”之前的字符 例:ORC+001 取ORC ...

  9. windows远程桌面连接树莓派

    1.树莓派上需要安装xrdp(An open source remote desktop protocol(rdp) server). sudo apt-get install xrdp 远程桌面打开 ...

  10. ajax 调用asp.net后台方法

    ajax 调用asp.net后台方法  这种做法有好几种,如调用xx.asxh 页面,或者直接调用xx.aspx也面,在page_Load中进行一些判断然后调用后面的其他方法, 或者你可以直接调用we ...