【bzoj1927】[Sdoi2010]星际竞速

来源:互联网 发布:平面转立体软件 编辑:程序博客网 时间:2024/04/29 23:05

题目描述:

10 年一度的银河系赛车大赛又要开始了。作为全银河最盛大的活动之一, 夺得这个项目的冠军无疑是很多人的梦想,来自杰森座 α星的悠悠也是其中之一。 赛车大赛的赛场由 N 颗行星和M条双向星际航路构成,其中每颗行星都有 一个不同的引力值。大赛要求车手们从一颗与这 N 颗行星之间没有任何航路的 天体出发,访问这 N 颗行星每颗恰好一次,首先完成这一目标的人获得胜利。 由于赛制非常开放,很多人驾驶着千奇百怪的自制赛车来参赛。这次悠悠驾 驶的赛车名为超能电驴,这是一部凝聚了全银河最尖端科技结晶的梦幻赛车。作 为最高科技的产物,超能电驴有两种移动模式:高速航行模式和能力爆发模式。 在高速航行模式下,超能电驴会展开反物质引擎,以数倍于光速的速度沿星际航 路高速航行。在能力爆发模式下,超能电驴脱离时空的束缚,使用超能力进行空 间跳跃——在经过一段时间的定位之后,它能瞬间移动到任意一个行星。 天不遂人愿,在比赛的前一天,超能电驴在一场离子风暴中不幸受损,机能 出现了一些障碍:在使用高速航行模式的时候,只能由每个星球飞往引力比它大 的星球,否则赛车就会发生爆炸。 尽管心爱的赛车出了问题,但是悠悠仍然坚信自己可以取得胜利。他找到了 全银河最聪明的贤者——你,请你为他安排一条比赛的方案,使得他能够用最少 的时间完成比赛。

输入:

第一行是两个正整数 N, M。 第二行 N 个数 A1~AN, 其中Ai表示使用能力爆发模式到达行星 i 所需的定位 时间。 接下来 M行,每行 3个正整数ui, vi, wi,表示在编号为 ui和vi的行星之间存 在一条需要航行wi时间的星际航路。 输入数据已经按引力值排序,也就是编号小的行星引力值一定小,且不会有 两颗行星引力值相同。

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

样例输入:

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

样例输出:

12

题解:

带瞬移的最小路径覆盖。拆点,费用流建图。

代码:

2016.04.06 指针版

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#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)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 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 1610#define maxm 60010#define INF 0x7fffffffstruct Edge{int from, to, w, c;Edge *next, *rev;}*last[maxn], *pre[maxn],tot[maxm], *ecnt = tot;int dis[maxn], s, t;long long ans;bool vis[maxn];std::queue<int> q;inline void link(R int _a, R int _b, R int _w, R int _c){*++ecnt = (Edge) {_a, _b, _w, _c, last[_a], ecnt + 1}; last[_a] = ecnt;*++ecnt = (Edge) {_b, _a, 0, -_c, last[_b], ecnt - 1}; last[_b] = ecnt;}inline bool spfa(){for (R int i = 0; i <= t; ++i) dis[i] = INF;q.push(s); vis[s] = 1; dis[s] = 0;while (!q.empty()){R int now = q.front(); q.pop();for (R Edge *iter = last[now]; iter; iter = iter -> next){if (iter -> w && iter -> c + dis[now] < dis[iter -> to]){dis[iter -> to] = iter -> c + dis[now];pre[iter -> to] = iter;if (!vis[iter -> to]){vis[iter -> to] = 1;//printf("%d\n",iter -> to );q.push(iter -> to);}}}vis[now] = 0;}return dis[t] != INF;}inline void mcf(){int x = INF;for (R Edge *iter = pre[t]; iter; iter = pre[iter -> from])cmin(x, iter -> w);for (R Edge *iter = pre[t]; iter; iter = pre[iter -> from]){ans += x * iter -> c;iter -> w -= x;iter -> rev -> w += x;}}int main(){//setfile();R int n = FastIn(), m = FastIn(); s = 0; t = n << 1 | 1;for (R int i = 1; i <= n; ++i){R int a = FastIn();link(s, i, 1, 0);link(i + n, t, 1, 0);link(s, i + n, 1, a);}for (R int i = 1; i <= m; ++i){R int _u = FastIn(), _v = FastIn(), _w = FastIn();link(dmin(_u, _v), dmax(_u, _v) + n, 1, _w);}while (spfa()) mcf();printf("%lld\n",ans );return 0;}


0 0
原创粉丝点击