01分数规划zoj2676(最优比例,最小割集+二分)
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 |
4 |
4 5 |
3 |
题意:给出一个网络连通图n个服务器m条网线以及费用,现在需要控制某些网线,令1发出的信号无论如何都不能到达n,且保证选择的网线总费用与网线总条数的比值最小,问需要选择的网线条数,并给出它们的序号;
#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(最优比例,最小割集+二分)的更多相关文章
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- 【转】[Algorithm]01分数规划
因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}
P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...
- POJ3757 01分数规划
题意: 有一个任务,给你提供n太服务器,让你在这n太服务器中选出k台完成这个任务,要求是每台服务器的工作时间相同,总的花费最小. 思路: 题目中给出对于每台服务器有这个式子: To ...
- 【BZOJ】4753: [Jsoi2016]最佳团体 01分数规划+树上背包
[题意]n个人,每个人有价值ai和代价bi和一个依赖对象ri<i,选择 i 时 ri 也必须选择(ri=0时不依赖),求选择k个人使得Σai/Σbi最大.n<=2500,ai,bi< ...
- codevs1183 泥泞的道路(01分数规划)
1183 泥泞的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description CS有n个小区,并且任意小区之间都有两 ...
- zoj 2676 二分+ISAP模板求实型参数的最小割(0-1分数规划问题)(可做ISAP模板)
/* 参考博文:http://www.cnblogs.com/ylfdrib/archive/2010/09/01/1814478.html 以下题解为转载代码自己写的: zoj2676 胡伯涛论文& ...
随机推荐
- 设置session失效时间
以X5部署在Tomcat上为例,说明如何设置session失效时间. 可以设置session失效时间的地点有三处,分别是 1.BusinessServer的session设置 \runtime\Bus ...
- yum 安装mysql5.6
系统centos5.5 进入http://dev.mysql.com/downloads/repo/,下载RedHat Enterprise Linux 5 / Oracle Linux 5版. 点击 ...
- 【转】四元数(Quaternion)和旋转
四元数介绍 旋转,应该是三种坐标变换--缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法--矩阵旋转和欧拉旋转. ...
- jquery的$.ajax async使用详解
async在jquery ajax中是一个同步参数了,我们下面来给大家介绍在jquery ajax中使用async时碰到的一些问题与方法介绍,希望例子能给各位同学带来一些帮助哦. async默 ...
- Python之list添加新元素、删除元素、替换元素
Python之list添加新元素 现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有 ...
- Windows下查看机器监听端口
1.查看所有端口占用情况 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 2.查看指定端口的占用情况 netstat -an |findstr :21
- Bootstrap 表单和图片 (内联表单,表单合组,水平排列,复选框和单选框,下拉列表,校验状态,添加额外的图标,控制尺寸,图片)
一.表单 基本格式 注:只有正确设置了输入框的 type 类型,才能被赋予正确的样式. 支持的输入框控件 包括:text.password.datetime.datetime-local.date.m ...
- Bash 快捷键大全
快捷键的一些说明: CTRL=C:这个键是指PC键盘上的Ctrl键 ALT=M:这个键是PC键盘上的ALT键,如果你键盘上没有这个键,可以尝试使用ESC键代替 SHIFT=S:此键是PC上的Shift ...
- php--yii2.0框架的curl
yii2.0框架的增删改查 //插入操作 save() $customer=new Customer(); $customer->name=‘小熊‘; $customer->save() ...
- JS-008-日期控件操作
此文以 js 操作 layDate 日期组件为示例演示,进行日期修改. 相应的演示示例脚本,敬请参阅之前的博文:HTML-001-日期组件 layDate 演示. 操作结果如下所示: 至此, JS-0 ...