题意:给出n(2<=n<=100)个城市之间的m(0<=m<=1000)条航线以及对应的机票价格,要求回答一些询问,每个询问是给出最大停留次数S,求从其实城市Calgary到终点城市Fredericton中途停留次数不超过s的最便宜的路程。
析:注意这个题是单向路,我还以为是双向的,dp[i][j] 当前在 i 城市,已经停留了 j 次, 用dijkstra 跑一次就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 100 + 10; const int maxm = 1e5 + 10; const int mod = 50007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } map<string, int> mp; int ID(const string &s){ if(mp.count(s)) return mp[s]; return mp[s] = mp.sz; } struct Edge{ int to, val, next; }; Edge edges[maxn*10<<1]; int head[maxn], cnt; void addEdge(int u, int v, int c){ edges[cnt].to = v; edges[cnt].val = c; edges[cnt].next = head[u]; head[u] = cnt++; } int dp[maxn][maxn]; bool vis[maxn]; int main(){ ios::sync_with_stdio(false); int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ cin >> n; int s, t; mp.cl; for(int i = 0; i < n; ++i){ string ss; cin >> ss; if(ss == "Calgary") s = ID(ss); else if(ss == "Fredericton") t = ID(ss); else ID(ss); } ms(head, -1); cnt = 0; cin >> m; while(m--){ string ss, tt; int d; cin >> ss >> tt >> d; int u = ID(ss), v = ID(tt); if(u == v) continue; addEdge(u, v, d); } ms(dp, INF); ms(vis, 0); dp[s][0] = 0; vis[s] = 1; queue<P> q; q.push(P(s, 0)); while(!q.empty()){ P p = q.front(); q.pop(); int u = p.fi, j = p.se; for(int i = head[u]; ~i; i = edges[i].next){ int v = edges[i].to; if(dp[v][j+1] > dp[u][j] + edges[i].val){ dp[v][j+1] = dp[u][j] + edges[i].val; q.push(P(v, j+1)); } } } if(kase > 1) cout << endl; cout << "Scenario #" << kase << endl; cin >> m; while(m--){ int x; cin >> x; ++x; int ans = INF; for(int i = 0; i <= x; ++i) ans = min(ans, dp[t][i]); if(ans == INF) cout << "No satisfactory flights\n"; else cout << "Total cost of flight(s) is $" << ans << endl; } } return 0; }