BZOJ1927 [SDOI2010] 星际竞速

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1927

Description

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

Input 

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

Output

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <queue>
 6 #define rep(i,l,r) for(int i=l; i<=r; i++)
 7 #define clr(x,y) memset(x,y,sizeof(x))
 8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
 9 using namespace std;
10 const int INF = 0x3f3f3f3f;
11 const int maxn = 1610;
12 inline int read(){
13     int ans = 0, f = 1;
14     char c = getchar();
15     for(; !isdigit(c); c = getchar())
16     if (c == '-') f = -1;
17     for(; isdigit(c); c = getchar())
18     ans = ans * 10 + c - '0';
19     return ans * f;
20 }
21 struct Edge{
22     Edge *pre,*rev; int to,cap,cost;
23 }edge[100010],*last[maxn],*cur[maxn],*pt;
24 int n,m,x,y,z,S,T,cost,d[maxn];
25 bool isin[maxn],vis[maxn];
26 queue <int> q;
27 inline void add(int x,int y,int z,int w){
28     pt->pre = last[x]; pt->to = y; pt->cap = z; pt->cost = w; last[x] = pt++;
29     pt->pre = last[y]; pt->to = x; pt->cap = 0; pt->cost = -w; last[y] = pt++;
30     last[x]->rev = last[y]; last[y]->rev = last[x];
31 }
32 bool spfa(){
33     clr(d,INF); d[S] = 0;
34     clr(isin,0); isin[S] = 1; q.push(S);
35     while (!q.empty()){
36         int now = q.front(); q.pop(); isin[now] = 0;
37         travel(now){
38             if (p->cap > 0 && d[p->to] > d[now] + p->cost){
39                 d[p->to] = d[now] + p->cost;
40                 if (!isin[p->to]) isin[p->to] = 1, q.push(p->to);
41             }
42         }
43     }
44     return d[T] != INF;
45 }
46 int dfs(int x,int flow){
47     if (x == T || !flow) return flow; vis[x] = 1; int w = 0;
48     for(Edge *p = cur[x]; p && w < flow; p = p->pre){
49         if (p->cap > 0 && d[p->to] == d[x] + p->cost && (!vis[p->to])){
50             int delta = dfs(p->to,min(p->cap,flow-w));
51             p->cap -= delta; p->rev->cap += delta; w += delta;
52             cost += delta * p->cost;
53             if (p->cap) cur[x] = p;
54         }
55     }
56     return w;
57 }
58 void mincost(){
59     while(spfa()){
60         rep(i,S,T) vis[i] = 0, cur[i] = last[i];
61         dfs(S,INF);
62     }
63 }
64 int main(){
65     n = read(); m = read();
66     clr(last,0); pt = edge; S = 0; T = n << 1 | 1;
67     rep(i,1,n){
68         x = read();
69         add(S,i+n,1,x); add(i+n,T,1,0); add(S,i,1,0);
70     }
71     rep(i,1,m){
72         x = read(); y = read(); z = read();
73         if (x > y) swap(x,y);
74         add(x,y+n,1,z);
75     }
76     mincost();
77     printf("%d
",cost);
78     return 0;
79 }
View Code
原文地址:https://www.cnblogs.com/jimzeng/p/bzoj1927.html