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

  

UVa 11280 Flying to Fredericton (DP + Dijkstra)的更多相关文章

  1. UVA 11280 - Flying to Fredericton SPFA变形

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

  2. UVA-11280 Flying to Fredericton (dijkstra)

    题目大意:一张有向图,n个节点,m条边,有边权.求从起点到终点在最多经过s个中间节点(不包括始末点)时的最小权和. 题目分析:因为起点和终点是固定的,只需一次dijkstra打出表dis[u][k], ...

  3. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  4. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  5. UVa 11367 Full Tank? (DP + Dijkstra)

    题意:n个城市有m条道路.每个城市的油价不一样,给出起点s和终点t,以及汽车的油箱的容量,求从城市s到城市 t 的最便宜路径. 析:dp[u][i] 表示在第 u 个城市,还剩下 i L升油,一开始用 ...

  6. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  7. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

  8. POJ3635 Full Tank?(DP + Dijkstra)

    题目大概说,一辆带有一个容量有限的油箱的车子在一张图上行驶,每行驶一单位长度消耗一单位油,图上的每个点都可以加油,不过都有各自的单位费用,问从起点驾驶到终点的最少花费是多少? 这题自然想到图上DP,通 ...

  9. UVa 103 - Stacking Boxes(dp求解)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

随机推荐

  1. as3 去掉字符串空白问题

    去掉内容的所有空白 function trim(str:String):String { })/g,""); } //[ ]内是一个中文空格一个英文空格 {}是说匹配一个到多个 / ...

  2. jquery实现背景图片动态适应

    背景 在我的一个项目中用到了背景图片,发现其中有一个问题,当页面长度动态变化时,原来能占满全部内容背景的图片会变得不能占满全部背景.如下图的效果: 这是正常的,背景图片占满全屏 当页面内容变长出现了滚 ...

  3. Haskell语言学习笔记(60)Biapplicative

    Biapplicative class Bifunctor p => Biapplicative p where bipure :: a -> b -> p a b (<< ...

  4. Mysql count+if 函数结合使用

    Mysql count+if 函数结合使用 果林椰子 关注 2017.05.18 13:48* 字数 508 阅读 148评论 0喜欢 1 涉及函数 count函数 mysql中count函数用于统计 ...

  5. delphi XE3解析JSON数据

    测试数据如下: Memo1.text中的数据: { "date":"周二(今天, 实时:12℃)", "dayPictureUrl":&qu ...

  6. Android中decode JPG时建议使用inPreferQualityOverSpeed

    在BitmapFactory.decodeBitmap方法中,参数BitmapFactory.Options里有一项是inPreferQualityOverSpeed:设为true的话,画质更好,加载 ...

  7. docker 配置远程访问证书验证

    centos7 生成证书 工具:openssl #cd /etc/docker   (docker的证书一般放这) #openssl genrsa -aes256 -passout pass:密码   ...

  8. -moz 火狐 -msIE -webkit[chrome safari]

    -moz代表firefox浏览器私有属性 -ms代表IE浏览器私有属性 -webkit代表chrome.safari私有属性

  9. java并发:AQS的简单理解

    简介: AQS全称 AbstractQueuedSynchronizer,提供了一个基于FIFO(先进先出)队列,可以用于构建锁或者其他相关同步装置的基础框架. ReentrantLock.Semap ...

  10. 不同的子序列 · Distinct Subsequences

    [抄题]: 给出字符串S和字符串T,计算S的不同的子序列中T出现的个数. 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响.(比如,“ACE”是“ ...