【BZOJ】2395: [Balkan 2011]Timeismoney
题解
最小乘积生成树!
我们把,x的总和和y的总和作为x坐标和y左边,画在坐标系上
我们选择两个初始点,一个是最靠近y轴的A,也就是x总和最小,一个是最靠近x轴的B,也就是y总和最小
连接两条直线,在这条直线上面的点都不用考虑了
我们选一个离直线最远的点C,且在直线下方,我们用叉积考虑这个东西,也就是……面积最大!我们如果用最小生成树的话,只要让面积是负的就好了
推一下式子,发现是\((A.y - B.y) * C.x + (B.x - A.x) * C.y\)我们发现就是把边设置成
\((A.y - B.y) * E[i].c + (B.x - A.x) * E[i].t\)做一遍最小生成树
找到C点后递归处理A,C和C,B即可
边界是两点连线下方没有点也就是叉积大于等于0
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <set>
//#define ivorysi
#define eps 1e-8
#define mo 974711
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define MAXN 10005
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef double db;
const int64 MOD = 1000000007;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) putchar('-');
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M;
struct Point {
int64 x,y;
int64 v;
Point(){};
Point(int64 _x,int64 _y) {
x = _x;y = _y;v = x * y;
}
friend bool operator < (const Point &a,const Point &b) {
return a.v < b.v || (a.v == b.v && a.x < b.x);
}
}ans;
struct Edge {
int u,v;
int64 c,t,w;
Edge(){}
Edge(int _u,int _v,int64 _c,int64 _t) {
u = _u;v = _v;c = _c;t = _t;
}
friend bool operator < (const Edge &a,const Edge &b) {
return a.w < b.w || (a.w == b.w && a.c < b.c);
}
}E[MAXN];
int fa[205];
int getfa(int u) {
return fa[u] == u ? u : fa[u] = getfa(fa[u]);
}
Point kruskal() {
sort(E + 1,E + M + 1);
Point res = Point(0,0);
for(int i = 1 ; i <= N ; ++i) fa[i] = i;
for(int i = 1 ; i <= M ; ++i) {
if(getfa(E[i].u) != getfa(E[i].v)) {
fa[getfa(E[i].u)] = getfa(E[i].v);
res.x += E[i].c;res.y += E[i].t;
}
}
res.v = res.x * res.y;
if(res < ans) ans = res;
return res;
}
void Work(Point A,Point B) {
for(int i = 1 ; i <= M ; ++i) {
E[i].w = (A.y - B.y) * E[i].c + (B.x - A.x) * E[i].t;
}
Point r = kruskal();
if((A.x - r.x) * (B.y - r.y) - (A.y - r.y) * (B.x - r.x) >= 0) return;
Work(A,r);
Work(r,B);
}
void Solve() {
read(N);read(M);
int u,v;
int64 c,t;
for(int i = 1 ; i <= M ; ++i) {
read(u);read(v);read(c);read(t);
++u;++v;
E[i] = Edge(u,v,c,t);
}
ans.v = 1e18;
for(int i = 1 ; i <= M ; ++i) {
E[i].w = E[i].c;
}
Point A = kruskal();
for(int i = 1 ; i <= M ; ++i) {
E[i].w = E[i].t;
}
Point B = kruskal();
Work(A,B);
printf("%lld %lld\n",ans.x,ans.y);
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
【BZOJ】2395: [Balkan 2011]Timeismoney的更多相关文章
- BZOJ 2395 [Balkan 2011]Timeismoney(最小乘积生成树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2395 [题目大意] 给出一张无向图,每条边上有a,b两个值,求生成树, 使得suma* ...
- bzoj 2395: [Balkan 2011]Timeismoney【计算几何+最小生成树】
妙啊,是一个逼近(?)的做法 把两个值最为平面上的点坐标,然后答案也是一个点. 首先求出可能是答案的点xy分别是按照c和t排序做最小生成树的答案,然后考虑比这两个点的答案小的答案,一定在xy连线靠近原 ...
- 【最小乘积生成树】bzoj2395[Balkan 2011]Timeismoney
设每个点有x,y两个权值,求一棵生成树,使得sigma(x[i])*sigma(y[i])最小. 设每棵生成树为坐标系上的一个点,sigma(x[i])为横坐标,sigma(y[i])为纵坐标.则问题 ...
- @bzoj - 2395@ [Balkan 2011]Timeismoney
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有n个城市(编号从0..n-1),m条公路(双向的),从中选择n ...
- bzoj 2395 [Balkan 2011]Timeismoney——最小乘积生成树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2395 如果把 \( \sum t \) 作为 x 坐标,\( \sum c \) 作为 y ...
- 【BZOJ2395】[Balkan 2011]Timeismoney
[BZOJ2395][Balkan 2011]Timeismoney 题面 \(darkbzoj\) 题解 如果我们只有一个条件要满足的话直接最小生成树就可以了,但是现在我们有两维啊... 我们将每个 ...
- 【BZOJ】3052: [wc2013]糖果公园
http://www.lydsy.com/JudgeOnline/problem.php?id=3052 题意:n个带颜色的点(m种),q次询问,每次询问x到y的路径上sum{w[次数]*v[颜色]} ...
- 【BZOJ】3319: 黑白树
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...
- 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...
随机推荐
- 编译的java工程压缩上传到linux服务器上后,中文的类名显示乱码
首先声明,类名是用中文命名的,这个别人写的,不允许修改. 本地用7zip软件压缩成zip包,传到服务器解压,发现中文的class文件名称是乱码. 解决办法: 方法一:使用jar命令打成jar包,传到服 ...
- Jekins - Hello world,Jekins + Maven + Git + Tomcat 的简单应用
Java Web 工程 新建一个简单的 Java Web 工程,并提交至 GitHub,可参考 Eclipse 提交工程至 GitHub 下载 jekins.war 在 http://mirrors. ...
- idea注册码激活防和谐
1.到网站 http://idea.lanyus.com/ 获取注册码: 2.修改hosts文件,位于C:\Windows\System32\drivers\etc,添加一行,win10推荐使用not ...
- @Resource,@Autowired,@Inject3种注入方式
概况 @Resource,@Autowired,@Inject 这3种都是用来注入bean的,它们属于不同的程序中. ANNOTATION PACKAGE SOURCE @Resource javax ...
- [php]php总结(2)
18.数组$arr[null] = 20; //array([]=>20)$arr[] = 20;//默认为下一个下标赋值unset()可以删除数组元素,但不会重建索引array_values( ...
- [gym100956]Problem J. Sort It! BIT+组合数
source : Pertozavodsk Winter Training Camp 2016 Day 1: SPb SU and SPb AU Contest, Friday, January 29 ...
- 渐变色之location概念.
CHENYILONG Blog 渐变色之location概念.全屏幕13-12-22 上午10:18 © chenyilong. Powered by Postach.io Blog
- Django rest framwork
Restful API REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类 ...
- 【译】第七篇 Replication:合并复制-订阅
本篇文章是SQL Server Replication系列的第七篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...
- numpy之ones,array,asarray
from:http://blog.csdn.net/gobsd/article/details/56485177 numpy.ones() 废话少说直接上代码 >>> np.ones ...