题目链接:https://cn.vjudge.net/problem/UVA-11280
#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
const int maxn =1e2+5;
const int mod=1e9+7;
int INF=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
map<string,int> mp;
string s1,s2;
int w,g[maxn][maxn],x;
int dp[maxn][maxn],n,m,q;
/*
Dijkstra变种
开个二维dp数组存储状态后
用优先队列去更新即可。
*/struct node
{int x,w;bool operator<(const node& y) const{return w>y.w;}
};
int main()
{int t;scanf("%d",&t);for(int ca=1;ca<=t;ca++){if(ca!=t) puts("");mp.clear();memset(g,0xf,sizeof(g));INF=g[0][0];scanf("%d",&n); for(int i=0;i<n;i++) cin>>s1,mp[s1]=i;scanf("%d",&m);for(int i=0;i<m;i++){cin>>s1>>s2>>w;int x=mp[s1],y=mp[s2];g[x][y]=min(g[x][y],w),g[y][x]=g[x][y];}memset(dp,0xf,sizeof(dp)); dp[0][0]=0;priority_queue<node> pq;pq.push(node{0,0});while(!pq.empty()){node tp=pq.top();pq.pop();int x=tp.x,w=tp.w;for(int i=0;i<n;i++){if(g[i][x]==INF) continue;if(i==x) continue;for(int j=0;j<=n;j++){if(dp[x][j]==INF) continue;if(j+1<=n&&dp[i][j+1]>dp[x][j]+g[i][x]){dp[i][j+1]=dp[x][j]+g[i][x];pq.push(node{i,dp[i][j+1]});}}}}puts("");printf("Scenario #%d\n",ca);scanf("%d",&q);for(int i=0;i<q;i++){scanf("%d",&x);x++;if(dp[n-1][x]!=INF) printf("Total cost of flight(s) is $");if(dp[n-1][x]==INF) puts("No satisfactory flights");else printf("%d\n",dp[n-1][x]);}}return 0;
}