题意:给出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;
}

  

最新文章

  1. snmp ubuntu/centos--
  2. Stack与Queue
  3. C++ 中 int 转string, 以及10进制转2进制
  4. JQuery学习笔记——JQuery基础
  5. Struts2原码分析系列之一
  6. c++适配器模式
  7. PHP - 设置地址栏小图标
  8. HTML target 属性
  9. Qt---自定义界面之QStyle
  10. JS 设计模式一 -- 原型模式
  11. mybatis 模糊查询 like的三种方式
  12. 单例模式的七种实现-Singleton(Java实现)
  13. java串口通信丢包
  14. python从零开始 -- 第2篇之python版本差异
  15. PhoneGap &amp; Cordova 安装白皮书
  16. linux 命令基础一。
  17. Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)
  18. 微信小程序wx.switchTab传参问题
  19. [Java] Hashtable 源码简要分析
  20. MySQL8.0的安装与配置(Windows 10)

热门文章

  1. python之函数的作用域
  2. mouseenter 事件
  3. jsonp 原理
  4. jenkins 修改工作目录
  5. 快速可靠网络传输协议 KCP(转)
  6. SecureCRT结合xmanager远程启动图形化界面程序
  7. Scala语言学习笔记(4)
  8. out对象以及网上答题系统
  9. python数据分析笔记——数据加载与整理]
  10. HttpClientUtil 工具类