[JLOI2011]飞行路线

[JLOI2011]飞行路线

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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

输入

数据的第一行有三个整数,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)

输出

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

样例输入

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

样例输出

8

提示

对于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.

solution:

    这题没说的,跑二维spfa,第一维为城市,第二维为使用的次数,转移和普通spfa相同。

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<iostream>
 5 using namespace std;
 6 __attribute__((optimize("O3")))int read() {
 7     int s=0,f=1;
 8     char ch=getchar();
 9     while(ch<'0'||ch>'9') {
10         if(ch=='-') {
11             f=-1;
12         }
13         ch=getchar();
14     }
15     while(ch>='0'&&ch<='9') {
16         s=(s<<1)+(s<<3)+(ch^48);
17         ch=getchar();
18     }
19     return s*f;
20 }
21 struct node {
22     int to,vv,next;
23 } c[100101];
24 struct oo {
25     int x,k;
26     friend bool operator < (oo a,oo b){
27         return (a.k==b.k)?(a.x>b.x):(a.k>b.k);
28     }
29 };
30 priority_queue<oo>q;
31 bool vis[10101][11];
32 int dis[10101][11],r[100001];
33 int tot;
34 __attribute__((optimize("O3")))void add(int x,int y,int vv) {
35     c[++tot]=(node) {
36         y,vv,r[x]
37     };
38     r[x]=tot;
39 }
40 int n,m,k,st,ed;
41 __attribute__((optimize("O3")))void spfa(){
42     memset(dis,0x6f,sizeof(dis));
43     dis[st][0]=0;
44     q.push((oo) {
45         st,0
46     });
47     vis[st][0]=true;
48     while (q.size()) {
49         oo s=q.top();
50         int now=s.x;
51         int tmp=s.k;
52         q.pop();
53         vis[now][tmp]=false;
54         for (int i=r[now]; i; i=c[i].next) {
55             if (c[i].vv+dis[now][tmp]<dis[c[i].to][tmp]) {
56                 dis[c[i].to][tmp]=c[i].vv+dis[now][tmp];
57                 //cout<<dis[c[i].tot][tmp]<<endl;
58                 if (!vis[c[i].to][tmp]) {
59                     vis[c[i].to][tmp]=true;
60                     q.push((oo) {
61                         c[i].to,tmp
62                     });
63                 }
64             }
65             if (dis[now][tmp]<dis[c[i].to][tmp+1]&&tmp<k) {
66                 dis[c[i].to][tmp+1]=dis[now][tmp];
67                 if (!vis[c[i].to][tmp+1]) {
68                     vis[c[i].to][tmp+1]=true;
69                     q.push((oo) {
70                         c[i].to,tmp+1
71                     });
72                 }
73             }
74         }
75     }
76 }
77 __attribute__((optimize("O3")))int main() {
78     n=read(),m=read(),k=read();
79     st=read()+1,ed=read()+1;
80     for (int i=1; i<=m; i++) {
81         int x=read()+1,y=read()+1,vv=read();
82         add(x,y,vv);
83         add(y,x,vv);
84     }
85     spfa();
86     int ans=0x7fffffff;
87     for (int i=0; i<=k; i++) {
88         if (dis[ed][i]<ans) {
89             ans=dis[ed][i];
90         }
91     }
92     printf("%d\n",ans);
93     return 0;
94 }

 

 

 

posted @ 2017-08-10 21:38  Forever_goodboy  阅读(217)  评论(0编辑  收藏  举报