[Luogu P2469] [BZOJ 1927] [SDOI2010]星际竞速

版权声明:欢迎转载蒟蒻博客,但请注明出处: https://blog.csdn.net/LPA20020220/article/details/84579614

洛谷传送门

BZOJ传送门

题目描述

10年一度的银河系赛车大赛又要开始了。作为全银河最盛大的活动之一,夺得这个项目的冠军无疑是很多人的梦想,来自杰森座α星的悠悠也是其中之一。

赛车大赛的赛场由 N N 颗行星和 M M 条双向星际航路构成,其中每颗行星都有一个不同的引力值。大赛要求车手们从一颗与这 N N 颗行星之间没有任何航路的天体出发,访问这 N N 颗行星每颗恰好一次,首先完成这一目标的人获得胜利。

由于赛制非常开放,很多人驾驶着千奇百怪的自制赛车来参赛。这次悠悠驾驶的赛车名为超能电驴,这是一部凝聚了全银河最尖端科技结晶的梦幻赛车。作为最高科技的产物,超能电驴有两种移动模式:高速航行模式和能力爆发模式。在高速航行模式下,超能电驴会展开反物质引擎,以数倍于光速的速度沿星际航路高速航行。在能力爆发模式下,超能电驴脱离时空的束缚,使用超能力进行空间跳跃——在经过一段时间的定位之后,它能瞬间移动到任意一个行星。

天不遂人愿,在比赛的前一天,超能电驴在一场离子风暴中不幸受损,机能出现了一些障碍:在使用高速航行模式的时候,只能由每个星球飞往引力比它大的星球,否则赛车就会发生爆炸。

尽管心爱的赛车出了问题,但是悠悠仍然坚信自己可以取得胜利。他找到了全银河最聪明的贤者——你,请你为他安排一条比赛的方案,使得他能够用最少的时间完成比赛。

输入输出格式

输入格式:

输入文件starrace.in的第一行是两个正整数 N N , M M

第二行 N N 个数 A 1 A N A_1\sim A_N ,其中Ai表示使用能力爆发模式到达行星 i i 所需的定位时间。

接下来 M M 行,每行 3 3 个正整数 u i , v i , w i u_i, v_i, w_i ,表示在编号为 u i u_i v i v_i 的行星之间存在一条需要航行 w i w_i 时间的星际航路。

输入数据已经按引力值排序,也就是编号小的行星引力值一定小,且不会有两颗行星引力值相同。

输出格式:

输出文件starrace.out仅包含一个正整数,表示完成比赛所需的最少时间。

输入输出样例

输入样例#1:

3 3
1 100 100
2 1 10
1 3 1
2 3 1

输出样例#1:

12

输入样例#2:

3 3
1 2 3
1 2 100
1 3 100
2 3 100

输出样例#2:

6

输入样例#3:

4 5
100 1000 10 100
1 2 100
2 3 100
4 3 100
1 3 20
2 4 20

输出样例#3:

230

说明

样例一说明:先使用能力爆发模式到行星 1 1 ,花费时间 1 1

然后切换到高速航行模式,航行到行星 2 2 ,花费时间 10 10

之后继续航行到行星 3 3 完成比赛,花费时间 1 1

虽然看起来从行星 1 1 到行星 3 3 再到行星 2 2 更优,但我们却不能那样做,因为那会导致超能电驴爆炸。

【数据规模和约定】

对于30%的数据 N 20 N≤20 M 50 M≤50

对于70%的数据 N 200 N≤200 M 4000 M≤4000

对于100%的数据 N 800 N≤800 M 15000 M≤15000 。输入数据中的任何数都不会超过 1 0 6 10^6

输入数据保证任意两颗行星之间至多存在一条航道,且不会存在某颗行星到自己的航道。

解题分析

带权的路径覆盖, 改成费用流就好了。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <queue>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 2050
#define S 0
#define INF 1e8
template <class T>
IN void in(T &x)
{
    x = 0; R char c = gc;
    for (; !isdigit(c); c = gc);
    for (;  isdigit(c); c = gc)
    x = (x << 1) + (x << 3) + c - 48;
}
template <class T> IN T min(T a, T b) {return a < b ? a : b;}
int n, m, cnt, T, ans;
int head[MX], pre[MX], dis[MX], del[MX];
bool inq[MX];
struct Edge {int to, cst, fl, nex;} edge[200500];
IN void add(R int from, R int to, R int fl, R int cst)
{
    edge[++cnt] = {to, cst, fl, head[from]}, head[from] = cnt;
    edge[++cnt] = {from, -cst, 0, head[to]}, head[to] = cnt;
}
namespace MCMF
{
    std::queue <int> q;
    IN bool SPFA()
    {
        std::memset(dis, 63, sizeof(dis));
        dis[S] = 0, del[S] = INF; R int now; q.push(S);
        W (!q.empty())
        {
            now = q.front(); q.pop();
            for (R int i = head[now]; ~i; i = edge[i].nex)
            {
                if (edge[i].fl && dis[edge[i].to] > dis[now] + edge[i].cst)
                {
                    dis[edge[i].to] = dis[now] + edge[i].cst;
                    del[edge[i].to] = min(del[now], edge[i].fl);
                    pre[edge[i].to] = i;
                    if (!inq[edge[i].to]) inq[edge[i].to] = true, q.push(edge[i].to);
                }
            }
            inq[now] = false;
        }
        return dis[T] < INF;
    }
    IN void updata()
    {
        R int now = T, pr;
        ans += del[T] * dis[T];
        W (now)
        {
            pr = pre[now];
            edge[pr].fl -= del[T];
            edge[pr ^ 1].fl += del[T];
            now = edge[pr ^ 1].to;
        }
    }
    IN void init()
    {
        W (SPFA()) updata();
        printf("%d", ans);
    }
}
int main(void)
{
    int foo, bar, buf;
    std::memset(head, cnt = -1, sizeof(head));
    in(n), in(m); T = n * 2 + 1;
    for (R int i = 1; i <= n; ++i) in(foo), add(S, i + n, 1, foo), add(S, i, 1, 0), add(i + n, T, 1, 0);
    for (R int i = 1; i <= m; ++i)
    {
        in(foo), in(bar), in(buf);
        if (foo > bar) std::swap(foo, bar);
        add(foo, bar + n, 1, buf);
    }
    MCMF::init();
}

猜你喜欢

转载自blog.csdn.net/LPA20020220/article/details/84579614