【bzoj2395】[Balkan 2011]Timeismoney
阿新 • • 發佈:2019-01-24
*題目描述:
有n個城市(編號從0..n-1),m條公路(雙向的),從中選擇n-1條邊,使得任意的兩個城市能夠連通,一條邊需要的c的費用和t的時間,定義一個方案的權值v=n-1條邊的費用和*n-1條邊的時間和,你的任務是求一個方案使得v最小
*輸入:
第一行兩個整數n,m,接下來每行四個整數a,b,c,t,表示有一條公路從城市a到城市b需要t時間和費用c
*輸出:
【output】timeismoney.out
僅一行兩個整數sumc,sumt,(sumc表示使得v最小時的費用和,sumc表示最小的時間和) 如果存在多個解使得sumc*sumt相等,輸出sumc最小的
*樣例輸出:
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
*樣例輸出:
279 501
*提示:
【資料規模】
1<=N<=200
1<=m<=10000
0<=a,b<=n-1
0<=t,c<=255
有5%的資料m=n-1
有40%的資料有t=c
對於100%的資料如上所述
*題解:
最小乘積生成樹。
我們把費用c和時間t看成二維平面上的點,然後我們要求的就是離原點最近的反比例函式影象上的點。我們先找到只以c和只以t為關鍵字的最小生成樹。答案一定是在這個三角形(這兩個點和這兩個點平行與x軸和平行與y軸的交點)上面。然後我們找到的離這條直線的最遠的點的座標更新答案,然後我們發現答案的範圍就又被我們縮小了!於是我們遞迴地做下去,直到找不到那樣的點為止。
*程式碼:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif
#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b), 0 : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b), 0 : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
#define maxn 210
#define maxm 10010
int n, m, Fa[maxn];
struct Poi
{
int x, y;
inline bool operator < (const Poi &that) const
{
return x * y == that.x * that.y ? x < that.x : x * y < that.x * that.y;
}
inline Poi operator - (const Poi &that) const
{
return (Poi) {x - that.x, y - that.y};
}
inline int operator * (const Poi &that) const
{
return x * that.y - y * that.x;
}
}ans, xx, yy;
struct Edge
{
int a, b, c, t, w;
inline bool operator < (const Edge &that) const {return w < that.w; }
}e[maxm];
int Find(R int x) {return Fa[x] == x ? x : Fa[x] = Find(Fa[x]); }
inline Poi mst()
{
std::sort(e + 1, e + m + 1);
for (R int i = 1; i <= n; ++i) Fa[i] = i;
R int ans1 = 0, ans2 = 0, ncnt = 0;
for (R int i = 1; i <= m && ncnt < n; ++i)
{
R int f1 = Find(e[i].a), f2 = Find(e[i].b);
if (f1 != f2)
{
ans1 += e[i].c;
ans2 += e[i].t;
Fa[f1] = f2;
++ncnt;
}
}
R Poi ret = (Poi) {ans1, ans2};
ans = dmin(ans, ret);
return ret;
}
void solve(R Poi a, R Poi b, R int dep)
{
for (R int i = 1; i <= m; ++i)
e[i].w = e[i].t * (b.x - a.x) - e[i].c * (b.y - a.y);
R Poi c = mst();
if ((b - a) * (c - a) < 0 && dep <= 10) solve(a, c, dep + 1), solve(c, b, dep + 1);
}
int main()
{
// setfile();
n = FastIn(), m = FastIn();
ans = (Poi) {23333, 66666};
for (R int i = 1; i <= m; ++i)
e[i] = (Edge) {FastIn() + 1, FastIn() + 1, FastIn(), FastIn(), 0};
for (R int i = 1; i <= m; ++i) e[i].w = e[i].c; xx = mst();
for (R int i = 1; i <= m; ++i) e[i].w = e[i].t; yy = mst();
solve(xx, yy, 0);
printf("%d %d\n", ans.x, ans.y );
return 0;
}