[SCOI2014]方伯伯运椰子
01分数规划思维题。
题中要求交通总量不减少,那么如果总量增加的话,总费用就会增加,所以一定不是更优的解。那么总量守恒。
这是不是就想到了网络流?对于每一个节点流入量等于流出量。然后就是很有思维的一个转化了:把压缩看成退流,把扩容看成增广。
边(x, y)一次压缩,就建一条y -> x,容量为a - d的边。
边(x, y)一次增广,就建一条x -> y,容量为b + d的边。也就是一次调整多出来的费用。那么这样建完图后,图中的一个环就代表一种调整方案!
回头看题,让求某一个比值最小,那一定会想到01分数规划,令ans = max((X - Y) / k),那么ans >= (X - Y) / k,于是有ans * k + Y - X >= 0。按上述的建图,Y - X就是环上的边权和,k就是环中的点数(边数),所以这个式子相当于每经过一条边,这条边的边权就加一个ans,那么ans * k + Y - X >= 0就可以写成Σ(ans + ei) >= 0。二分的时候,如果存在负环,说明mid取小了,向右二分;否则向左二分。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 5e3 + ;
const int maxe = 3e3 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
struct Edge
{
int nxt, to, w;
}e[maxe << ];
int head[maxn], ecnt = -;
void addEdge(int x, int y, int w)
{
e[++ecnt] = (Edge){head[x], y, w};
head[x] = ecnt;
} db dis[maxn];
bool vis[maxn], mak[maxn];
bool spfa(int now, db x)
{
vis[now] = mak[now] = ;
for(int i = head[now]; i != -; i = e[i].nxt)
{
if(dis[e[i].to] > dis[now] + e[i].w + x)
{
dis[e[i].to] = dis[now] + e[i].w + x;
if(vis[e[i].to]) return ;
if(spfa(e[i].to, x)) return ;
}
}
vis[now] = ;
return ;
} bool judge(db x)
{
Mem(vis, ); Mem(mak, );
for(int i = ; i <= n + ; ++i)
if(!mak[i])
{
if(spfa(i, x)) return ;
}
return ;
} int main()
{
Mem(head, -);
n = read(); m = read();
for(int i = ; i <= m; ++i)
{
int x = read(), y = read(), a = read(), b = read(), c = read(), d = read();
if(c) addEdge(y, x, a - d);
addEdge(x, y, b + d);
}
db L = , R = 1e5;
while(R - L > eps)
{
db mid = (L + R) / 2.00;
if(judge(mid)) L = mid;
else R = mid;
}
printf("%.2lf\n", L);
return ;
}
[SCOI2014]方伯伯运椰子的更多相关文章
- bzoj 3597: [Scoi2014]方伯伯运椰子 0/1分数规划
3597: [Scoi2014]方伯伯运椰子 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 144 Solved: 78[Submit][Status ...
- bzoj 3597: [Scoi2014]方伯伯运椰子 [01分数规划 消圈定理 spfa负环]
3597: [Scoi2014]方伯伯运椰子 题意: from mhy12345 给你一个满流网络,对于每一条边,压缩容量1 需要费用ai,扩展容量1 需要bi, 当前容量上限ci,每单位通过该边花费 ...
- bzoj3597[Scoi2014]方伯伯运椰子 01分数规划+spfa判负环
3597: [Scoi2014]方伯伯运椰子 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 594 Solved: 360[Submit][Statu ...
- 3597: [Scoi2014]方伯伯运椰子[分数规划]
3597: [Scoi2014]方伯伯运椰子 Time Limit: 30 Sec Memory Limit: 64 MB Submit: 404 Solved: 249 [Submit][Sta ...
- bzoj 3597: [Scoi2014]方伯伯运椰子
Description Input 第一行包含二个整数N,M 接下来M行代表M条边,表示这个交通网络 每行六个整数,表示Ui,Vi,Ai,Bi,Ci,Di 接下来一行包含一条边,表示连接起点的边 Ou ...
- bzoj 3597 [Scoi2014] 方伯伯运椰子 - 费用流 - 二分答案
题目传送门 传送门 题目大意 给定一个费用流,每条边有一个初始流量$c_i$和单位流量费用$d_i$,增加一条边的1单位的流量需要花费$b_i$的代价而减少一条边的1单位的流量需要花费$a_i$的代价 ...
- 2019.03.28 bzoj3597: [Scoi2014]方伯伯运椰子(01分数规划)
传送门 题意咕咕咕有点麻烦不想写 思路: 考虑加了多少一定要压缩多少,这样可以改造边. 于是可以通过分数规划+spfaspfaspfa解决. 代码: #include<bits/stdc++.h ...
- BZOJ3597 SCOI2014方伯伯运椰子(分数规划+spfa)
即在总流量不变的情况下调整每条边的流量.显然先二分答案变为求最小费用.容易想到直接流量清空跑费用流,但复杂度略有些高. 首先需要知道(不知道也行?)一种平时基本不用的求最小费用流的算法——消圈法.算法 ...
- Bzoj3597: [Scoi2014]方伯伯运椰子
题面 传送门 Sol 消圈定理:如果一个费用流网络的残量网络有负环,那么这个费用流不优 于是这个题就可以建出残量网络,然后分数规划跑负环了 # include <bits/stdc++.h> ...
随机推荐
- Oracle RAC集群搭建(zero)--全是报错
1. 提示Check if the DISPLAYvariable is set. Failed<<<< 解决方案: #xhost + //切换到root用户输入 #s ...
- pat09-散列1. Hashing (25)
09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...
- centos6.5 源码编译 mysql5.6.21
1.yum安装各个依赖包 [root@WebServer ~]# yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automa ...
- 无监督学习(Unsupervised Learning)
无监督学习(Unsupervised Learning) 聚类无监督学习 特点 只给出了样本, 但是没有提供标签 通过无监督学习算法给出的样本分成几个族(cluster), 分出来的类别不是我们自己规 ...
- HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- (一)安装Python
一.安装python 打开 Python官网,找到“Download”, 在其下拉菜单中选择自己的平台(Windows/Mac),一般的Linux平台已经自带的Python,所以不需要安装,通过打开“ ...
- axios请求报Uncaught (in promise) Error: Request failed with status code 404
使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...
- c# winform 欢迎界面时加载数据
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- js.css嵌入dll
处理请求,返回 public ActionResult Get() { //传递一个部分名称 var n = Request["n"]; n = n.Replace('/', '. ...
- python学习(六)--正则的一些例子
import re #正则表达式#compile函数,--将正则表达式转变为内部函数,提高执行效率strr = "python123456"pattern = "Pyth ...