[JLOI 2011]飞行路线&[USACO 09FEB]Revamping Trails

Description

Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?

Input

数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)

Output

只有一行,包含一个整数,为最少花费。

Sample Input

5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100

Sample Output

8

HINT

对于30%的数据,2<=n<=50,1<=m<=300,k=0;

对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;

对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.

题解

题面放的是$[JLOI 2011]$飞行路线,这两道题一毛一样。区别就是$USACO$的数据$k<=20$,并且$s=1$,$t=n$。

建立分层图。

$f[u][t]$表示在节点u时已经免费乘坐t次的最少花费。照样跑最短路。

枚举与$u$相连的所有节点$v$,$w(u,v)$表示权值。
若$t<k$:

$$f[v][t+1]=min(f[v][t+1],f[u][t])$$

对于所有:

$$f[v][t]=min(f[v][t],f[u][t]+w(u,v))$$

由于$USACO$数据范围大了点,$STL$的优先队列还过不了,手打了个堆$A$了。

(注意代码中标红的地方二选一)

 1 #include <set>
 2 #include <map>
 3 #include <ctime>
 4 #include <cmath>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <string>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 #define LL long long
15 #define Max(a, b) ((a) > (b) ? (a) : (b))
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 using namespace std;
18 const int INF = ~0u>>1;
19 const int N = 10000;
20 const int M = 50000;
21 
22 int s, t;
23 struct tt{
24     int to, cost, next;
25 }edge[M*2+5];
26 int path[N+5], top;
27 int n, m, k, u, v, c;
28 struct node{
29     int cost, u, t;
30     node () {}
31     node (int _cost, int _u, int _t) {cost = _cost, u = _u, t = _t;}
32     bool operator < (const node &b) const{
33       return cost > b.cost;
34     }
35 };
36 priority_queue<node>Q;
37 int f[N+5][25];
38 
39 void add(int u, int v, int c){
40     edge[++top].to = v;
41     edge[top].next = path[u];
42     edge[top].cost = c;
43     path[u] = top;
44 }
45 void dijkstra(){
46     memset(f, 127/3, sizeof(f));
47     f[s][0] = 0;
48     Q.push(node(0, s, 0));
49     while (!Q.empty()){
50       node tmp = Q.top(); Q.pop();
51       for (int i = path[tmp.u]; i; i=edge[i].next){
52           if (tmp.t < k && f[edge[i].to][tmp.t+1] > f[tmp.u][tmp.t]){
53             f[edge[i].to][tmp.t+1] = f[tmp.u][tmp.t];
54             Q.push(node(f[edge[i].to][tmp.t+1], edge[i].to, tmp.t+1));
55           }
56           if (f[edge[i].to][tmp.t] > f[tmp.u][tmp.t]+edge[i].cost){
57             f[edge[i].to][tmp.t] = f[tmp.u][tmp.t]+edge[i].cost;
58             Q.push(node(edge[i].to, edge[i].to, tmp.t));
59           }
60       }
61     }
62 }
63 
64 int main(){
65     scanf("%d%d%d", &n, &m, &k);
66     scanf("%d%d", &s, &t);//[JLOI 2011]飞行路线
67     s = 1, t = n;//[USACO 09FEB]Revamping Trails
68     for (int i = 1; i <= m; i++){
69       scanf("%d%d%d", &u, &v, &c);
70       add(u, v, c);
71       add(v, u, c);
72     }
73     dijkstra();
74     printf("%d
", f[t][k]);
75     return 0;
76 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/7473258.html