题意
给定一些国家,和两个国家间的花费,现在有一些询问,询问每次最多转k次飞机,最小花费
分析
最短路的裸题,跑spfa或者dijsktra什么的都行
多开一维来记录转k次飞机时的最短路是什么(拆点?)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue> using namespace std;
const int maxn=+;
const int maxm=+;
const int INF=;
int T,m,n,sz,q;
map<string,int>name;
string num[maxn];
string s;
int d[maxn][maxn];
int vis[maxn][maxn];
int head[maxn],to[maxm],val[maxm],ecnt,Next[maxm];
struct Node{
int u,cnt;
};
void spfa(int s){
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=n+;j++)
d[i][j]=INF;
d[s][]=;
vis[s][]=;
queue<Node>q;
q.push((Node){s,});
while(!q.empty()){
Node x=q.front();q.pop();
int u=x.u,cnt=x.cnt;
vis[u][cnt]=;
for(int i=head[u];i;i=Next[i]){
int v=to[i];
if(d[v][cnt+]>d[u][cnt]+val[i]){
d[v][cnt+]=d[u][cnt]+val[i];
if(!vis[v][cnt+]){
vis[v][cnt+]=;
q.push((Node){v,cnt+});
}
}
}
}
}
void add_edge(int a,int b,int w){
ecnt++;
to[ecnt]=b;
val[ecnt]=w;
Next[ecnt]=head[a];
head[a]=ecnt;
}
int main(){
//freopen("out.txt","w",stdout);
scanf("%d",&T);
for(int t=;t<=T;t++){
if(t!=)printf("\n");
memset(head,,sizeof(head));
printf("Scenario #%d\n",t);
sz=;
ecnt=;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>s;
sz++;
name[s]=sz;
num[sz]=s;
}
scanf("%d",&m);
for(int i=;i<=m;i++){
string a,b;
int w;
cin>>a>>b>>w;
add_edge(name[a],name[b],w);
}
spfa(); scanf("%d",&q);
int a;
for(int i=;i<=q;i++){
scanf("%d",&a);
int ans=INF;
for(int j=;j<=min(a,n);j++){
ans=min(ans,d[n][j+]);
}
if(ans>=INF){
printf("No satisfactory flights\n");
}else
printf("Total cost of flight(s) is $%d\n",ans);
}
}
return ;
}