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)是目标式的变形,且当F(e)=0时,a就是aveCost,以cost[i][j]-a为容量建图,那么此时F(e)就是最小割容量.

二分确定最小割也即最大流为0时的a值,当流量恰好为0时取值最优,否则,若流量大于0不是最优,流量小于0不满足题意

注意:当确定a值时,cost[i][j]-a会导致负容量边,这些边可以使F(e)更小,所以直接加上即可

 #include <cstdio>
#include <cstring>
#include<algorithm>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=;
const int maxm=;
const int inf=0x7fffffff;
const double eps=1e-;
int n,m;
int G[maxn][maxn];
int e[maxn][maxn];
int len[maxn];
int num[maxn];
int ind[maxn][maxn];
double c[maxn][maxn];
double f[maxn][maxn];
int ans[maxm];
int alen;
bool pars[maxn];
int dis[maxn];
int gap[maxn]; void addedge(int f,int t){
G[f][len[f]++]=t;
G[t][len[t]++]=f;
}
double build(double lamda){
double flow=;
memset(len,,sizeof(len));
for(int i=;i<=n;i++){
for(int j=;j<num[i];j++){
int to=e[i][j];
if(i<to){
f[i][to]=c[i][to]-lamda;
f[to][i]=c[i][to]-lamda;
if(f[i][to]<){flow+=f[i][to];}
else{
addedge(i,to);
}
}
}
}
memset(dis,,sizeof(dis));
memset(gap,,sizeof(gap));
gap[]=n;
return flow;
} double dfs(int s,double flow){
if(s==n)return flow;
int mindis=n-;
double tflow=flow,sub;
for(int i=;i<len[s];i++){
int to=G[s][i];
if(f[s][to]>eps){
if(dis[to]+==dis[s]){
sub=dfs(to,min(tflow,f[s][to]));
f[s][to]-=sub;
f[to][s]+=sub;
tflow-=sub;
if(dis[]>=n)return flow-tflow;
if(tflow<eps)break;
}
mindis=min(mindis,dis[to]);
}
}
if(flow-tflow<eps){
--gap[dis[s]];
if(gap[dis[s]]==)dis[]=n;
else {
dis[s]=mindis+;
++gap[dis[s]];
}
}
return flow-tflow;
} double maxflow(double lamda){
double flow=build(lamda);
while(dis[]<n){
flow+=dfs(,inf);
}
return flow;
} double binarysearch(double s,double e){
if(s+eps>e){return s;}
double mid=(s+e)/;
double flow=maxflow(mid);
if(fabs(flow)<eps)return mid;
else if(flow<-eps){
return binarysearch(s,mid);
}
else {
return binarysearch(mid,e);
}
} void fnd(double a){
memset(pars,,sizeof(pars));
queue<int >que;
que.push();
pars[]=true;
while(!que.empty()){
int s=que.front();que.pop();
for(int i=;i<len[s];i++){
int to=G[s][i];
if(!pars[to]&&f[s][to]>eps){
pars[to]=true;que.push(to);
}
}
}
alen=;
for(int i=;i<=n;i++){
if(pars[i]){
for(int j=;j<num[i];j++){
int to=e[i][j];
if(!pars[to]){
ans[alen++]=ind[i][to];
}
else if(i<to&&c[i][to]+eps<a){
ans[alen++]=ind[i][to];
}
}
}
else {
for(int j=;j<num[i];j++){
int to=e[i][j];
if(i<to&&!pars[to]&&c[i][to]+eps<a){
ans[alen++]=ind[i][to];
}
}
}
}
sort(ans,ans+alen);
} int main(){
bool first=true;
while(scanf("%d%d",&n,&m)==){
if(!first)puts("");
else first=false;
memset(num,,sizeof(num));
int maxc=,minc=1e7+;
for(int i=;i<=m;i++){
int f,t,cost;
scanf("%d%d%d",&f,&t,&cost);
e[f][num[f]++]=t;
e[t][num[t]++]=f;
c[f][t]=c[t][f]=cost;
ind[f][t]= ind[t][f]=i;
maxc=max(maxc,cost);
minc=min(minc,cost);
}
double a=binarysearch(,maxc+);
fnd(a);
printf("%d\n",alen);
for(int i=;i<alen;i++)printf("%d%c",ans[i],i==alen-?'\n':' ');
}
return ;
}

HDU 2676 Network Wars 01分数规划,最小割 难度:4的更多相关文章

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

    题目详解出自 论文 Amber-最小割模型在信息学竞赛中的应用 题目大意: 给出一个带权无向图 G = (V,E), 每条边 e属于E都有一个权值We,求一个割边集C,使得该割边集的平均边权最小,即最 ...

  2. ZOJ 2676 Network Wars[01分数规划]

    ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special J ...

  3. bzoj 3232 圈地游戏 —— 01分数规划+最小割建图(最大权闭合子图)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 心烦意乱的时候调这道题真是...越调越气,就这样过了一晚上... 今天再认真看看,找出 ...

  4. ZOJ 2676 Network Wars(网络流+分数规划)

    传送门 题意:求无向图割集中平均边权最小的集合. 论文<最小割模型在信息学竞赛中的应用>原题. 分数规划.每一条边取上的代价为1. #include <bits/stdc++.h&g ...

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

    Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include < ...

  6. 【BZOJ3232】圈地游戏 分数规划+最小割

    [BZOJ3232]圈地游戏 Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意 ...

  7. bzoj 3232: 圈地游戏【分数规划+最小割】

    数组开小导致TTTTTLE-- 是分数规划,设sm为所有格子价值和,二分出mid之后,用最小割来判断,也就是判断sm-dinic()>=0 这个最小割比较像最大权闭合子图,建图是s像所有点连流量 ...

  8. 【洛谷P2494】 [SDOI2011]保密(分数规划+最小割)

    洛谷 题意: 题意好绕好绕...不想写了. 思路: 首先类似于分数规划做法,二分答案得到到每个点的最小危险度. 然后就是在一个二分图中,两边撤掉最少的点(相应代价为上面算出的危险度)及相应边,使得中间 ...

  9. 洛谷2494 [SDOI2011]保密 (分数规划+最小割)

    自闭一早上 分数规划竟然还能被卡精度 首先假设我们已经知道了到每个出入口的时间(代价) 那我们应该怎么算最小的和呢? 一个比较巧妙的想法是,由于题目规定的是二分图. 我们不妨通过最小割的形式. 表示这 ...

随机推荐

  1. [转载] Python的GIL是什么鬼,多线程性能究竟如何

    原文: http://cenalulu.github.io/python/gil-in-python/ GIL是什么 首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器( ...

  2. json与jsonp应用及其他ajax数据交互方式

    1.json是数据交换格式,使用实例如下: $.getJSON( '/manage/asset/asset_delByIds.action', { 'ids':id }, function(data) ...

  3. 阻塞与非阻塞的IO网络读写

    看我之前的文章就知道,一般对于网络读的socket,都会加上O_NONBLOCK,非阻塞的选项. int setnonblocking(int fd) { int old_option = fcntl ...

  4. 【Todo】Nginx架构学习

    要进行Web服务,绕不开的就是Nginx.这已经是大型网站的标配.对Nginx进行一定程度的深入学习. http://www.ituring.com.cn/article/4436 http://bl ...

  5. JS重要知识点(转载 学习中。。。)

    这里列出了一些JS重要知识点(不全面,但自己感觉很重要).彻底理解并掌握这些知识点,对于每个想要深入学习JS的朋友应该都是必须的. 讲解还是以示例代码搭配注释的形式,这里做个小目录: JS代码预解析原 ...

  6. J2EE 第二阶段项目(八)

    类别统计差不多完成了! 还有个地区统计了!

  7. 自己学习smarty的一些代码 和记录

    http://www.yiibai.com/smarty/smarty_install.html  手册可以看这里 index.tpl <!DOCTYPE html> <html&g ...

  8. xcode 脚本编译,打包ipa

    1.清理工程 /usr/bin/xcodebuild -target targetName clean 2.编译 /usr/bin/xcodebuild -target targetName buil ...

  9. base64 数据处理

    base64 数据处理 1. base64 字母表 2. 原理 处理原理 http://baike.baidu.com/view/469071.htm 3. iOS上的应用 iOS7 之前使用http ...

  10. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型

    Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...