Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?
标签:
在我看来相当于拆点
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<vector> 7 #include<queue> 8 #define rep(i,l,r) for(int i=l;i<r;i++) 9 #define clr(a,x) memset(a,x,sizeof(a)) 10 using namespace std; 11 struct edge{ 12 int to,v; 13 }; 14 struct node{ 15 int num,d,f; 16 bool operator <(const node&q)const{ 17 return d>q.d; 18 } 19 }; 20 const int maxn=10005,maxm=50005,maxk=15; 21 int n,m,k,s,t,d[maxn][maxk]; 22 bool p[maxn][maxk]; 23 priority_queue<node>q; 24 vector<edge>e[maxn]; 25 int main() 26 { 27 clr(p,0); 28 node start,end; 29 start.d=0; 30 start.f=0; 31 cin>>n>>m>>k>>start.num>>end.num; 32 rep(i,0,n) 33 rep(j,0,k+1) 34 d[i][j]=10000000; 35 rep(i,0,k+1) d[start.num][i]=0; 36 rep(i,0,m){ 37 edge ed; 38 int from; 39 scanf("%d%d%d",&from,&ed.to,&ed.v); 40 e[from].push_back(ed); 41 swap(from,ed.to); 42 e[from].push_back(ed); 43 } 44 q.push(start); 45 int cnt=0; 46 while(!q.empty()){ 47 cnt++; 48 node now=q.top(); 49 q.pop(); 50 if(p[now.num][now.f]) continue; 51 p[now.num][now.f]=1; 52 p[now.num][now.f]=1; 53 rep(i,0,e[now.num].size()){ 54 if(now.d+e[now.num][i].v<d[e[now.num][i].to][now.f]){ 55 d[e[now.num][i].to][now.f]=now.d+e[now.num][i].v; 56 { 57 node next; 58 next.f=now.f; 59 next.d=d[e[now.num][i].to][now.f]; 60 next.num=e[now.num][i].to; 61 q.push(next); 62 } 63 } 64 if(now.d<d[e[now.num][i].to][now.f+1]){ 65 d[e[now.num][i].to][now.f+1]=now.d; 66 { 67 node next; 68 next.f=now.f+1; 69 next.d=now.d; 70 next.num=e[now.num][i].to; 71 q.push(next);; 72 } 73 } 74 } 75 } 76 printf("%d",d[end.num][k]); 77 return 0; 78 }
对于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.
标签:
原文地址:http://www.cnblogs.com/chensiang/p/4623112.html