【BZOJ 2395】Time is money

时间:2023-03-09 23:26:08
【BZOJ 2395】Time is money
题目大意
有n个城市(编号从0..n-1),m条公路(双向的),
从中选择n-1条边,使得任意的两个城市能够连通,一条边需要的c的费用和t的时间,
定义一个方案的权值v=n-1条边的费用和*n-1条边的时间和,你的任务是求一个方案使得v最小

Input

第一行两个整数n,m,接下来每行四个整数a,b,c,t,表示有一条公路从城市a到城市b需要t时间和费用c
Output

【output】timeismoney.out
仅一行两个整数sumc,sumt,(sumc表示使得v最小时的费用和,sumc表示最小的时间和) 如果存在多个解使得sumc*sumt相等,输出sumc最小的
Sample Input

5 7

0 1 161 79

0 2 161 15

0 3 13 153

1 4 142 183

2 4 236 80

3 4 40 241

2 1 65 92

Sample Output

279 501

HINT

【数据规模】

1<=N<=200

1<=m<=10000

0<=a,b<=n-1

0<=t,c<=255

有5%的数据m=n-1

有40%的数据有t=c

对于100%的数据如上所述

我们把每个生成树的解(∑t,∑c)=>设为坐标(t,c),k=tc,构成反比例函数

要使k最小,c=k/t尽可能贴近t轴

所以从下凸包中找一个最小的

那么首先最极端的是单纯按t排序或按c排序得到的点A,B

先找到离(A,B)最远的C,在递归(A,C)和(C,B),边界为AC×BC>=0

这样就可以求出所有下凸包上的值

离AB最远的C必定导致SΔABC最大

即AB×AC最大,把常数去掉得到一个关于C的公式,把它带入最小生成树,算出答案

丢个有图的链接,不过边界好像有点问题

http://blog.csdn.net/regina8023/article/details/45246933

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
struct Node
{
int u,v;
lol t,c,w;
}E[];
struct ZYYS
{
lol x,y;
}A,B,ans;
int n,m,set[];
bool cmp(Node a,Node b)
{
return a.w<b.w;
}
int find(int x)
{
if (set[x]!=x) set[x]=find(set[x]);
return set[x];
}
ZYYS kruskal()
{lol i,cnt;
lol anst,ansc;
ZYYS C;
anst=;ansc=;
for (i=;i<=n;i++)
set[i]=i;
cnt=;
for (i=;i<=m;i++)
{
lol p=find(E[i].u),q=find(E[i].v);
if (p!=q)
{
set[p]=q;
cnt++;
anst+=E[i].t;
ansc+=E[i].c;
if (cnt==n-) break;
}
}
return (ZYYS){ansc,anst};
}
lol cross(ZYYS p,ZYYS a,ZYYS b)
{
return (a.x-p.x)*(b.y-p.y)-(b.x-p.x)*(a.y-p.y);
}
void solve(ZYYS a,ZYYS b)
{ZYYS C;
lol i;
lol y=a.y-b.y,x=b.x-a.x;
for (i=;i<=m;i++)
E[i].w=E[i].t*x+E[i].c*y;
sort(E+,E+m+,cmp);
C=kruskal();
if (C.x*C.y<ans.x*ans.y||(ans.x*ans.y==C.x*C.y&&C.x<ans.x))
ans=C;
if (cross(C,a,b)>=) return;
solve(a,C);
solve(C,b);
}
int main()
{

lol i;
cin>>n>>m;
ans.x=ans.y=1e9;
for (i=;i<=m;i++)
{
scanf("%d%d%lld%lld",&E[i].u,&E[i].v,&E[i].c,&E[i].t);
E[i].u++;
E[i].v++;
}
for (i=;i<=m;i++)
E[i].w=E[i].c;
sort(E+,E+m+,cmp);
A=kruskal();
if (A.x*A.y<ans.x*ans.y)
ans=A;
for (i=;i<=m;i++)
E[i].w=E[i].t;
sort(E+,E+m+,cmp);
B=kruskal();
if (B.x*B.y<ans.x*ans.y)
ans=B;
solve(A,B);
cout<<ans.x<<' '<<ans.y;
}