bzoj 2763 [JLOI2011]飞行路线

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.

 
考虑到k十分小,发现免费的话就是费用为0,所以可以将其拆成k个平面,
上面的层可以进入下面相邻的层,花费为0建边,然后k个终点,随便到
然后就ok。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int d[10001][11],head[10001],q[100001][2];
 6 int n,m,k,cnt,st,ed;
 7 bool inq[10001][11];
 8 struct data{int to,next,v;}e[100001];
 9 void ins(int u,int v,int w)
10 {e[++cnt].to=v;e[cnt].v=w;e[cnt].next=head[u];head[u]=cnt;}
11 void spfa()
12 {
13     int t=0,w=1;
14     memset(d,127/3,sizeof(d));
15     q[0][0]=st;q[0][1]=0;
16     inq[st][0]=1;d[st][0]=0;
17     while(t!=w)
18     {
19         int now=q[t][0],tmp=q[t++][1];
20         if(t==100001)t=0;
21         for(int i=head[now];i;i=e[i].next)
22         {
23             int to=e[i].to;
24             if(d[now][tmp]+e[i].v<d[to][tmp])
25             {
26                 d[to][tmp]=d[now][tmp]+e[i].v;
27                 if(!inq[to][tmp])
28                 {
29                     inq[to][tmp]=1;
30                     q[w][0]=to;q[w++][1]=tmp;
31                     if(w==100001)w=0;
32                 }
33             }
34             if(d[now][tmp]<d[to][tmp+1]&&tmp<k)
35             {
36                 d[to][tmp+1]=d[now][tmp];
37                 if(!inq[to][tmp+1])
38                 {
39                     inq[to][tmp+1]=1;
40                     q[w][0]=to;q[w++][1]=tmp+1;
41                     if(w==100001)w=0;
42                 }
43             }
44         }
45         inq[now][tmp]=0;
46     }
47     int ans=0x7fffffff;
48     for(int i=0;i<=k;i++)
49        ans=min(ans,d[ed][i]);
50     printf("%d",ans);
51 }
52 int main()
53 {
54     scanf("%d%d%d%d%d",&n,&m,&k,&st,&ed);
55     for(int i=1;i<=m;i++)
56     {
57         int u,v,w;
58         scanf("%d%d%d",&u,&v,&w);
59         ins(u,v,w);ins(v,u,w);
60     }
61     spfa();
62     return 0;
63 }
原文地址:https://www.cnblogs.com/fengzhiyuan/p/7598957.html