[JLOI 2011]飞行路线[USACO 09FEB]Revamping Trails
Description
Alice和Bob現(xiàn)在要乘飛機(jī)旅行,他們選擇了一家相對(duì)便宜的航空公司。該航空公司一共在n個(gè)城市設(shè)有業(yè)務(wù),設(shè)這些城市分別標(biāo)記為0到n-1,一共有m種航線,每種航線連接兩個(gè)城市,并且航線有一定的價(jià)格。Alice和Bob現(xiàn)在要從一個(gè)城市沿著航線到達(dá)另一個(gè)城市,途中可以進(jìn)行轉(zhuǎn)機(jī)。航空公司對(duì)他們這次旅行也推出優(yōu)惠,他們可以免費(fèi)在最多k種航線上搭乘飛機(jī)。那么Alice和Bob這次出行最少花費(fèi)多少?Input
數(shù)據(jù)的第一行有三個(gè)整數(shù),n,m,k,分別表示城市數(shù),航線數(shù)和免費(fèi)乘坐次數(shù)。 第二行有兩個(gè)整數(shù),s,t,分別表示他們出行的起點(diǎn)城市編號(hào)和終點(diǎn)城市編號(hào)。(0<=s,t<n) 接下來有m行,每行三個(gè)整數(shù),a,b,c,表示存在一種航線,能從城市a到達(dá)城市b,或從城市b到達(dá)城市a,價(jià)格為c。(0<=a,b<n,a與b不相等,0<=c<=1000)Output
只有一行,包含一個(gè)整數(shù),為最少花費(fèi)。Sample Input
5 6 10 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
8HINT
對(duì)于30%的數(shù)據(jù),2<=n<=50,1<=m<=300,k=0;
對(duì)于50%的數(shù)據(jù),2<=n<=600,1<=m<=6000,0<=k<=1;
對(duì)于100%的數(shù)據(jù),2<=n<=10000,1<=m<=50000,0<=k<=10.
題解
題面放的是$[JLOI 2011]$飛行路線,這兩道題一毛一樣。區(qū)別就是$USACO$的數(shù)據(jù)$k<=20$,并且$s=1$,$t=n$。
建立分層圖。
$f[u][t]$表示在節(jié)點(diǎn)u時(shí)已經(jīng)免費(fèi)乘坐t次的最少花費(fèi)。照樣跑最短路。
枚舉與$u$相連的所有節(jié)點(diǎn)$v$,$w(u,v)$表示權(quán)值。
若$t<k$:
$$f[v][t+1]=min(f[v][t+1],f[u][t])$$
對(duì)于所有:
$$f[v][t]=min(f[v][t],f[u][t]+w(u,v))$$
由于$USACO$數(shù)據(jù)范圍大了點(diǎn),$STL$的優(yōu)先隊(duì)列還過不了,手打了個(gè)堆$A$了。
(注意代碼中標(biāo)紅的地方二選一)
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\n", f[t][k]); 75 return 0; 76 }?
轉(zhuǎn)載于:https://www.cnblogs.com/NaVi-Awson/p/7473258.html
總結(jié)
以上是生活随笔為你收集整理的[JLOI 2011]飞行路线[USACO 09FEB]Revamping Trails的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装nginx+ngx_lua支持WAF
- 下一篇: [中英对照]How PCI Works