分数规划的裸题。

不会分数规划的OIer。百度:胡伯涛《最小割模型在信息学竞赛中的应用》

  1. /*
  2. TLE1:
  3. last:add(i,j+n,1e9,(real)((real)a[i][j]-ans*(real)b[i][j]));
  4. now :add(i,j+n,1,(real)((real)a[i][j]-ans*(real)b[i][j]));
  5. TLE2:
  6. last:real l=eps,r=(real)u/(real)v,mid,ans=0,now;
  7. now :real l=0,r=(real)(u+v-1)/(real)v,mid,ans=0,now;
  8. TLE3:
  9. last:struct edge{int v,next;real cost,cap;}e[N<<1];
  10. now :to[N],next[N],cap[N];real cost[N];(结构体跑得慢)
  11. TLE4:
  12. last:typedef long double real;
  13. now :typedef double real;
  14. */
  15. #include<cmath>
  16. #include<cstdio>
  17. #include<cstring>
  18. #include<iostream>
  19. #define set(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
  20. using namespace std;
  21. const int N=1e5+;
  22. const int Z=;
  23. typedef long double real;
  24. const real eps=1e-;
  25. int n,m,S,T,a[Z][Z],b[Z][Z];
  26. int to[N],next[N],cap[N];real cost[N];int tot=,head[N];
  27. real dis[N];int q[N],pre[N];bool vis[N];
  28. inline int read(){
  29. int x=,f=;char ch=getchar();
  30. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  31. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  32. return x*f;
  33. }
  34. void add(int x,int y,int z,real Cost){
  35. to[++tot]=y;cap[tot]=z;cost[tot]=Cost;next[tot]=head[x];head[x]=tot;
  36. to[++tot]=x;cap[tot]=;cost[tot]=-Cost;next[tot]=head[y];head[y]=tot;
  37. }
  38. bool spfa(){
  39. memset(vis,,sizeof vis);
  40. for(int i=;i<=T+;i++) dis[i]=-1e9;
  41. unsigned short h=,t=;q[t]=S;dis[S]=;
  42. while(h!=t){
  43. int x=q[++h];vis[x]=;
  44. for(int i=head[x];i;i=next[i]){
  45. if(cap[i]>&&dis[to[i]]<dis[x]+cost[i]){
  46. dis[to[i]]=dis[x]+cost[i];
  47. pre[to[i]]=i;
  48. if(!vis[to[i]]){
  49. vis[to[i]]=;
  50. q[++t]=to[i];
  51. }
  52. }
  53. }
  54. }
  55. return dis[T]>-1e9;
  56. }
  57. real augment(){
  58. // int flow=1e9;
  59. // for(int i=T;i!=S;i=to[pre[i]^1]) flow=min(flow,cap[pre[i]]);
  60. int flow=;
  61. for(int i=T;i!=S;i=to[pre[i]^]){
  62. cap[pre[i]]-=flow;
  63. cap[pre[i]^]+=flow;
  64. }
  65. return dis[T]*flow;
  66. }
  67. real mapping(real ans){
  68. tot=;S=;T=n<<|;
  69. memset(head,,sizeof head);
  70. for(int i=;i<=n;i++) add(S,i,,),add(i+n,T,,);
  71. for(int i=;i<=n;i++){
  72. for(int j=;j<=n;j++){
  73. add(i,j+n,,(real)((real)a[i][j]-ans*(real)b[i][j]));
  74. }
  75. }
  76. real res=0.0;
  77. while(spfa()) res+=augment();
  78. return res;
  79. }
  80. int cmp(real x){
  81. if(fabs(x)<eps) return ;
  82. return x>?:-;
  83. }
  84. void work1(){
  85. tot=;S=;T=n<<|;
  86. memset(head,,sizeof head);
  87. for(int i=;i<=n;i++) add(S,i,,),add(i+n,T,,);
  88. for(int i=;i<=n;i++){
  89. for(int j=;j<=n;j++){
  90. add(i,j+n,,(real)a[i][j]);
  91. }
  92. }
  93. real res=0.0;
  94. while(spfa()) res+=augment();
  95. res/=1.0*n;
  96. printf("%.6lf\n",(double)res);
  97. }
  98. int main(){
  99. set(ball);
  100. n=read();
  101. int u=,v=;
  102. for(int i=,t;i<=n;i++){
  103. t=;
  104. for(int j=;j<=n;j++){
  105. a[i][j]=read();
  106. t=max(t,a[i][j]);
  107. }
  108. u+=t;
  109. }
  110. bool f=;
  111. for(int i=,t;i<=n;i++){
  112. t=1e9;
  113. for(int j=;j<=n;j++){
  114. b[i][j]=read();
  115. t=min(t,b[i][j]);
  116. if(b[i][j]!=) f=;
  117. }
  118. v+=t;
  119. }
  120. if(f){work1();return ;}
  121. real l=,r=(real)(u+v-)/(real)v,mid,ans=,now;
  122. while(l+eps<r){
  123. mid=(l+r)/2.0;
  124. now=mapping(mid);
  125. if(cmp(now)==){printf("%.6lf\n",(double)mid);return ;}
  126. if(cmp(now)>) l=mid;
  127. else r=mid;
  128. }
  129. printf("%.6lf\n",(double)l);
  130. fclose(stdin);
  131. fclose(stdout);
  132. return ;
  133. }

codevs 5965 [SDOI2017]新生舞会的更多相关文章

  1. [Sdoi2017]新生舞会 [01分数规划 二分图最大权匹配]

    [Sdoi2017]新生舞会 题意:沙茶01分数规划 貌似\(*10^7\)变成整数更科学 #include <iostream> #include <cstdio> #inc ...

  2. BZOJ_4819_[Sdoi2017]新生舞会_01分数规划+费用流

    BZOJ_4819_[Sdoi2017]新生舞会_01分数规划+费用流 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞 ...

  3. 洛谷 P3705 [SDOI2017]新生舞会 解题报告

    P3705 [SDOI2017]新生舞会 题目描述 学校组织了一次新生舞会,\(Cathy\)作为经验丰富的老学姐,负责为同学们安排舞伴. 有\(n\)个男生和\(n\)个女生参加舞会买一个男生和一个 ...

  4. 【BZOJ 4819】 4819: [Sdoi2017]新生舞会 (0-1分数规划、二分+KM)

    4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 601  Solved: 313 Description 学校 ...

  5. 【BZOJ4819】[Sdoi2017]新生舞会 01分数规划+费用流

    [BZOJ4819][Sdoi2017]新生舞会 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女 ...

  6. [BZOJ4819][SDOI2017]新生舞会(分数规划+费用流,KM)

    4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1097  Solved: 566[Submit][Statu ...

  7. 【算法】01分数规划 --- HNOI2009最小圈 & APIO2017商旅 & SDOI2017新生舞会

    01分数规划:通常的问法是:在一张有 \(n\) 个点,\(m\) 条边的有向图中,每一条边均有其价值 \(v\) 与其代价 \(w\):求在图中的一个环使得这个环上所有的路径的权值和与代价和的比率最 ...

  8. 4819: [Sdoi2017]新生舞会(分数规划)

    4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1031  Solved: 530[Submit][Statu ...

  9. 题解:SDOI2017 新生舞会

    题解:SDOI2017 新生舞会 Description 学校组织了一次新生舞会,Cathy 作为经验丰富的老学姐,负责为同学们安排舞伴. 有 \(n\) 个男生和 \(n\) 个女生参加舞会.一个男 ...

随机推荐

  1. Android编程的写法规范

    一.获取View对象 1.private EditText m_txtSmsPhone = null;//定义一个EditText控件的调用对象 m_表示为View对象 txt表示文本框 2.在pro ...

  2. 读写SQLServer数据库中的image类型数据(简单)

    1.将double类型的数据存储于image类型的变量中: (1). char *CManualForecastResultBll::DoubleArray2Binary(std::vector< ...

  3. ASIHTTPRequest 简单使用

    ASIHTTPResquest 框架功能强大,应用非常多. 曾经写过ASIHTTPResquest的导入,如今就看一下基本使用 记一下当中基础的操作: 1.发送同步请求: NSURL * url = ...

  4. php getallheaders使用注意事项

    This function is an alias for apache_request_headers(). Please read the apache_request_headers() doc ...

  5. Atitit.数据库事务隔离级别 attilax 总结

    Atitit.数据库事务隔离级别 1. 事务隔离级别的作用 1 2. 在的隔离级别 2 3. 常见数据库的默认管理级别 3 1. 事务隔离级别的作用 较低的隔离级别可以增强许多用户同时访问数据的能力, ...

  6. 【Android】14.3 浏览手机中的所有文件夹和文件

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 前面我们了解了内部存储.外部存储的含义,用一句话说,内部存储实际上是保存在"data"文件夹下 ...

  7. bazel-编译多目标

    demo2 使用bazel编译多目标示例,一个bianry,一个library. demo2目录树 ── demo2 ​ ├── app ​ │ ├── BUILD ​ │ ├── func.cpp ...

  8. 第7章 Iptables与Firewalld防火墙。

    第7章 Iptables与Firewalld防火墙.     Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00     ...

  9. QQ连连看-外挂

    QQ连连看-外挂 2014-11-06 参考 [1] [视频教程] c语言实践课程之qq连连看辅助开发 [2] CE工具下载 [3] [原创]qq连连看外挂制作详解

  10. lua工具库penlight--07函数编程(二)

    列表压缩 列表压缩是以紧凑的方式通过指定的元素创建表.在 Python里,你可以说: ls = [x for x in range(5)]  # == [0,1,2,3,4] 在 Lua,使用pl.c ...