【bzoj2395】[Balkan 2011]Timeismoney

*题目描述:
有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;
}
posted @ 2016-05-26 14:34  cot  阅读(197)  评论(0编辑  收藏  举报