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 胡伯涛论文& ...
随机推荐
- Java 基本数据类型转换
1. Integer/int 转 String Integer a = 1; (int a = 1) String b = Integer.toString(a); 2. float 转 Strin ...
- Android之Dialog详解
Android中的对话框形式大致可分为五种:分别是一般对话框形式,列表对话框形式,单选按钮对话框,多选按钮对话框,自定义对话框. 在实际开发中,用系统的对话框会很少,因为太丑了,美工不愿意,多是使用自 ...
- 在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程
http://jijiaa12345.iteye.com/blog/1739754 在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程 本文是一篇在Myeclipse下构建Jav ...
- wampserver
- docker sonaqube
postgresql: image: orchardup/postgresql:latest environment: - POSTGRESQL_USER=sonar - POSTGR ...
- MVC HTML辅助类常用方法记录
(1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(modelItem => item.Titl ...
- Qt 之 自定义提示信息框—迅雷风格(模拟QDialog类的exec()方法) good
http://blog.csdn.net/goforwardtostep/article/details/53614830
- Windows下Memcache的安装与在php中使用
memcache dll插件和测试例子下载地址: http://pecl.php.net/package/memcache Windows下Memcache的安装方法 Memcached官方:http ...
- 针对JD-GUI
switch(0){ case 1001: JSONObject jsoObj; String data = null; boolean isclose = false; try{ jsoObj = ...
- Linux some command(continue...)
挂载硬盘 sudo mount -t ext4 /dev/sdb1 /media/hadoop 自动挂载相关 sudo blkid sudo fdisk -l vim /etc/fstab cat / ...