题意
给定一些国家,和两个国家间的花费,现在有一些询问,询问每次最多转k次飞机,最小花费
分析
最短路的裸题,跑spfa或者dijsktra什么的都行
多开一维来记录转k次飞机时的最短路是什么(拆点?)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <map> 6 #include <queue> 7 8 using namespace std; 9 const int maxn=100+10; 10 const int maxm=1000+10; 11 const int INF=2147000000; 12 int T,m,n,sz,q; 13 map<string,int>name; 14 string num[maxn]; 15 string s; 16 int d[maxn][maxn]; 17 int vis[maxn][maxn]; 18 int head[maxn],to[maxm],val[maxm],ecnt,Next[maxm]; 19 struct Node{ 20 int u,cnt; 21 }; 22 void spfa(int s){ 23 memset(vis,0,sizeof(vis)); 24 for(int i=1;i<=n;i++) 25 for(int j=1;j<=n+1;j++) 26 d[i][j]=INF; 27 d[s][0]=0; 28 vis[s][0]=1; 29 queue<Node>q; 30 q.push((Node){s,0}); 31 while(!q.empty()){ 32 Node x=q.front();q.pop(); 33 int u=x.u,cnt=x.cnt; 34 vis[u][cnt]=0; 35 for(int i=head[u];i;i=Next[i]){ 36 int v=to[i]; 37 if(d[v][cnt+1]>d[u][cnt]+val[i]){ 38 d[v][cnt+1]=d[u][cnt]+val[i]; 39 if(!vis[v][cnt+1]){ 40 vis[v][cnt+1]=1; 41 q.push((Node){v,cnt+1}); 42 } 43 } 44 } 45 } 46 } 47 void add_edge(int a,int b,int w){ 48 ecnt++; 49 to[ecnt]=b; 50 val[ecnt]=w; 51 Next[ecnt]=head[a]; 52 head[a]=ecnt; 53 } 54 int main(){ 55 //freopen("out.txt","w",stdout); 56 scanf("%d",&T); 57 for(int t=1;t<=T;t++){ 58 if(t!=1)printf("\n"); 59 memset(head,0,sizeof(head)); 60 printf("Scenario #%d\n",t); 61 sz=0; 62 ecnt=0; 63 scanf("%d",&n); 64 for(int i=1;i<=n;i++){ 65 cin>>s; 66 sz++; 67 name[s]=sz; 68 num[sz]=s; 69 } 70 scanf("%d",&m); 71 for(int i=1;i<=m;i++){ 72 string a,b; 73 int w; 74 cin>>a>>b>>w; 75 add_edge(name[a],name[b],w); 76 } 77 spfa(1); 78 79 scanf("%d",&q); 80 int a; 81 for(int i=1;i<=q;i++){ 82 scanf("%d",&a); 83 int ans=INF; 84 for(int j=0;j<=min(a,n);j++){ 85 ans=min(ans,d[n][j+1]); 86 } 87 if(ans>=INF){ 88 printf("No satisfactory flights\n"); 89 }else 90 printf("Total cost of flight(s) is $%d\n",ans); 91 } 92 } 93 return 0; 94 }