BZOJ 2763 [JLOI2011]飞行路线

题面:

2763: [JLOI2011]飞行路线

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3204  Solved: 1224
[Submit][Status][Discuss]

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.

 

令f[i][j]为到i,免费乘坐j次的最小花费。

之后跑堆优化二维spfa

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 #define INF 0x7fffffff
 6 struct node
 7 {
 8     int u,v,ne,ll;
 9 }sh[100001];
10 int ad[100001],book[12][10001],di[12][10001],n,m,k,e;
11 int s,t;
12 struct pre
13 {
14     int x,k;
15     bool operator < (const pre &a) const
16     {
17         return di[k][x]>di[a.k][a.x];
18     }   
19 }kk;
20 priority_queue<pre>ss;
21 inline void add(int u,int v,int l)
22 {
23     sh[e]=(node){u,v,ad[u],l};
24     ad[u]=e++;
25 }
26 inline void spfa(int x)
27 {
28     int no,tt,j;
29     di[0][x]=0;
30     ss.push((pre){x,0});
31     book[0][x]=1;
32     while(!ss.empty())
33     {
34         kk=ss.top();
35         no=kk.x;
36         tt=kk.k;
37         ss.pop();
38         for(j=ad[no];j!=-1;j=sh[j].ne)
39         {
40             if(di[tt][sh[j].v]>di[tt][no]+sh[j].ll)
41             {
42                 di[tt][sh[j].v]=di[tt][no]+sh[j].ll;
43                 if(!book[tt][sh[j].v])
44                 {
45                     ss.push((pre){sh[j].v,tt});
46                     book[tt][sh[j].v]=1; 
47                 }
48             }
49             if(di[tt][no]<di[tt+1][sh[j].v]&&tt<k)
50             {
51                 di[tt+1][sh[j].v]=di[tt][no];
52                 if(!book[tt+1][sh[j].v])
53                 {
54                     ss.push((pre){sh[j].v,tt+1});
55                     book[tt+1][sh[j].v]=1;    
56                 }
57             }
58         }
59         book[tt][no]=0;
60     }
61 }
62 int main()
63 {
64     int x,y,z;
65     scanf("%d%d%d",&n,&m,&k);
66     scanf("%d%d",&s,&t);
67     memset(ad,-1,sizeof(ad));
68     int i,j;
69     for(i=1;i<=m;i++)
70     {
71         scanf("%d%d%d",&x,&y,&z);
72         add(x,y,z);
73         add(y,x,z);
74     }
75     for(i=0;i<=n;++i)
76         for(j=0;j<=k;++j)
77             di[j][i]=INF;
78     spfa(s);
79     int tot=INF;
80     printf("%d",di[k][t]);
81 }
BZOJ 2763
原文地址:https://www.cnblogs.com/radioteletscope/p/7350539.html