[COGS 2401]Time is Money
Description
给你 \(n\) 个节点 \(m\) 条边的无向连通图。每条边有两个权值 \(c,t\) ,要你生成一棵边集为 \(\mathbb{E}\) 的生成树使得 \[\sum_{e\in \mathbb{E}}e_c \times \sum_{e\in \mathbb{E}}e_t\] 最小。
\(1\leq n\leq 200,1\leq m\leq 10000\)
Solution
令 \(x=\sum_{e\in \mathbb{E}}e_c,y=\sum_{e\in \mathbb{E}}e_t\) 。记 \(k=xy\) ,那么对于反比例函数 \(y=\frac{k}{x}\) , \(k\) 越小越贴近坐标轴。
我们将所有生成树的权值以 \((x,y)\) 的形式刻画在坐标轴上,显然满足条件的最小值一定在左下凸包上。
那么首先最极端的是单纯按 \(c\) 排序和按 \(t\) 排序得到的点 \(A,B\) 。
然后显然要找到在连线 \(AB\) 左下最远的点 \(C\) 。
由于离 \(AB\) 最远的 \(C\) 必定导致 \(S_{\Delta ABC}\) 最大。
显然就是求 \(\left(\vec{AB}\times\vec{AC}\right)_{min}\) \[\begin{aligned}\vec{AB}\times\vec{AC}&=(x_B-x_A)(y_C-y_A)-(y_B-y_A)(x_C-x_A)\\&=y_C(x_B-x_A)+x_C(y_A-y_B)+K\end{aligned}\]
其中 \(K\) 是与 \(C\) 无关的量,可以忽略。所以将所有边权改为上述式子后做一遍最小生成树就可以求出 \(C\) 了。
然后继续分治对线段 \(AC\) 和线段 \(CB\) 求解即可。递归的边界为 \(\vec{AB}\times\vec{AC}\geq 0\) 。
Code
//It is made by Awson on 2018.3.7
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 200, M = 10000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, m, fa[N+5];
struct tt {
int u, v, w, c, t;
bool operator < (const tt &b) const {return w < b.w; }
}edge[M+5];
struct node {
int x, y;
node(int _x = 0, int _y = 0) {x = _x, y = _y; }
int operator * (const node &b) const {return x*b.y-y*b.x; }
node operator + (const node &b) {node t; t.x = x+b.x, t.y = y+b.y; return t; }
node operator - (const node &b) {node t; t.x = x-b.x, t.y = y-b.y; return t; }
void print() {write(x), putchar(' '), writeln(y); }
}ans, A, B;
int find(int x) {return fa[x] ? fa[x] = find(fa[x]) : x; }
void update(node &ans, node x) {if (1ll*ans.x*ans.y > 1ll*x.x*x.y || (1ll*ans.x*ans.y == 1ll*x.x*x.y && ans.x > x.x)) ans = x; }
bool judge(node A, node B, node C) {return (B-A)*(C-A) >= 0; }
node kruskal() {
memset(fa, 0, sizeof(fa)); int cnt = 0; node tmp;
sort(edge+1, edge+1+m);
for (int i = 1; i <= m; i++) {
if (find(edge[i].u)^find(edge[i].v)) {
++cnt; fa[find(edge[i].u)] = find(edge[i].v);
tmp = tmp+node(edge[i].c, edge[i].t);
if (cnt == n-1) break;
}
}
return tmp;
}
void doit(node A, node B) {
int x = B.x-A.x, y = A.y-B.y; node C;
for (int i = 1; i <= m; i++) edge[i].w = x*edge[i].t+y*edge[i].c;
update(ans, C = kruskal());
if (judge(A, B, C)) return;
doit(A, C), doit(C, B);
}
void work() {
read(n), read(m);
for (int i = 1; i <= m; i++) read(edge[i].u), read(edge[i].v), read(edge[i].c), read(edge[i].t), ++edge[i].u, ++edge[i].v;
for (int i = 1; i <= m; i++) edge[i].w = edge[i].c;
A = ans = kruskal();
for (int i = 1; i <= m; i++) edge[i].w = edge[i].t;
update(ans, B = kruskal());
doit(A, B);
ans.print();
}
int main() {
work(); return 0;
}
[COGS 2401]Time is Money的更多相关文章
- [HNOI 2014]画框
Description 题库链接 \(T\) 组询问,每组询问给你个 \(2\times N\) 的带权二分图,两个权值 \(a,b\) ,让你做匹配使得 \[\sum a\times \sum b\ ...
- 【COGS 254】【POI 2001】交通网络图
http://www.cogs.top/cogs/problem/problem.php?pid=254 dist[i]表示能最早到达i点的时间.这样就可以用最短路模型来转移了. #include&l ...
- 【COGS】894. 追查坏牛奶
http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...
- 【COGS】147. [USACO Jan08] 架设电话线(二分+spfa)
http://cojs.tk/cogs/problem/problem.php?pid=147 学到新姿势了orz 这题求的是一条1-n的路径的最大路径最小. 当然是在k以外的. 我们可以转换一下. ...
- 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)
http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...
- 【COGS & USACO】896. 圈奶牛(凸包)
http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...
- 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)
http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...
- Cogs 97. [NOIP2007] 树网的核 Floyd
题目: http://cojs.tk/cogs/problem/problem.php?pid=97 97. [NOIP2007] 树网的核 ★☆ 输入文件:core.in 输出文件:core ...
- bzoj 2401: 陶陶的难题I 数论
2401: 陶陶的难题I Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 89 Solved: 24[Submit][Status] Descript ...
随机推荐
- (译文)开始学习Vue.js特性--Scoped Slots
什么是scoped slots A scoped slot is a special type of slot that functions as a reusable template (that ...
- HIVE的常用操作(HQL)语句
HIVE基本操作命令 创建数据库 >create database db_name; >create database if not exists db_name;//创建一个不存在的数据 ...
- oralce数据库常用到的一些sql命令(加字段注释,修改数据之类)
最近开始接触oralce,整理了一下最近使用 pl/sql 常用到的一些sql命令 1.修改表中的数据 编写查询语句及条件,然后加上"FOR UPDATE","FOR U ...
- x64系统安装ODAC问题经验分享
64bit系统安装ODAC经验分享 背景: 最近项目里面有用到 WCF+Entity Framework+oracle 这个架构用过的朋友应该都知道,Entity Framework要通过ODAC的方 ...
- C语言指针作业
一.PTA实验作业 题目1:6-5 判断回文字符串 1. 本题PTA提交列表 2. 设计思路 3.代码截图 4.本题调试过程碰到问题及PTA提交列表情况说明. 第一次做的时候我j直接等于count,其 ...
- Flask 学习 十一 关注者
数据库关系 1.1多对多关系 添加第三张表(关联表),多对多关系可以分解成原表和关联表之间的两个一对多的关系 多对多仍然使用db.relationship()方法定义,但是secondary参数必须设 ...
- v7000数据恢复_MDisk重建数据恢复方法(北亚数据恢复)
很多工程师都有这样的疑问,MDisk重建后还能不能恢复数据呢?应该怎么做才能恢复数据呢?本文这里就以IBM V7000存储为例,详细讲解因为某个MDisk被重建导致的数据丢失的恢复方法.我们本案例中的 ...
- js中严格模式
我们在js中可以使用"use strict";定义了我们在接下来的文档输写中 将按照严格模式进行: function(){ "use strict'; ;// 在这里我们 ...
- node.js的安装的配置
一.Node.js 安装配置 Node.js 提供在Windows和Linux上安装 1. Window 上安装Node.js 64 位安装包下载地址 : https://nodejs.org/di ...
- ELK学习总结(4-2)关于导入数据
用REST API的_bulk来批量插入,可以达到5到10w条每秒 把数据写进json文件,然后再通过批处理,执行文件插入数据: 1.先定义一定格式的json文件,文件不能过大,过大会报错 2.后用c ...