codeforces 189D AlgoRace
来源:互联网 发布:java 字符串截取 编辑:程序博客网 时间:2024/05/01 04:37
有m种车,给定每种车从城市i到城市j的时间话费cost[i][j],有 r 个询问,每次询问从 i 到 j ,最多换 k 次车类型的最小时间花费
用cost[i][j][k]表示用第 i 种车,从j 走到 k的最小花费,读入花费矩阵之后,注意用floyd预处理,保证cost[i][j][k]是最优。然后用f[i][j][k] 表示换i 次车从 j 到k 的最优解,然后每次询问直接输出就行
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define rep(i, j, k) for(ll i = j; i <= k; i++)#define ll long long#define maxn 1000009#define inf 0x7fffffff/2using namespace std;int n, m, r, f[1009][61][61], cost[62][62][62];int main (){cin >> n >> m >> r;rep (i, 1, m)rep (j, 1, n)rep (k, 1, n)scanf ("%d", &cost[i][j][k]);rep (i, 1, m)rep (k, 1, n)rep (u, 1, n)rep (v, 1, n)cost[i][u][v] = min (cost[i][u][v], cost[i][u][k] + cost[i][k][v]);rep (i, 0, n)rep (j, 1, n)rep (k, 1, n)f[i][j][k] = inf;rep (k, 1, m)rep (i, 1, n)rep (j, 1, n)rep (w, 1, n)f[0][i][j] = min (f[0][i][j], cost[k][i][w] + cost[k][w][j]);/*cout << endl;for (int i = 1; i <= n; i++, cout << endl)rep (j, 1, n)printf ("%d ", f[0][i][j]);*/rep (i, 1, n)rep (w, 1, n)rep (j, 1, n)rep (k, 1, n)f[i][j][k] = min (f[i][j][k], f[i - 1][j][w] + f[0][w][k]);while (r--){int u, v, w;scanf ("%d%d%d", &u, &v, &w);w = min (w, n);int ret = inf;rep (i, 0, w)ret = min (ret, f[i][u][v]);printf ("%d\n", ret);}return 0;}
0 0
- codeforces 189D AlgoRace
- Codeforces 189D AlgoRace floyd+DP
- CodeForces Round #119 (187B) - AlgoRace
- Codeforces Round #119 (Div. 1) B. AlgoRace
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- Codeforces 275D
- Codeforces 126D
- Codeforces 337d
- Codeforces D. Points
- CodeForces 292D Connected Components (并查集+YY)
- UML简介
- CSU 1808 地铁【最短路变形】
- Node.js技术原理
- 大数据IMF传奇行动绝密课程第56课:揭秘Spark SQL和DataFrame的本质
- codeforces 189D AlgoRace
- 【PHP】Yii2中事务的使用以及代码实例
- 大数据IMF传奇行动绝密课程第57课:Spark SQL on Hive配置及实战
- static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较
- 用命令行开关UAC
- very good
- mysql数据库的增删改查(菜鸟用来总结学习的,高手勿进)
- 开启我的IT博客第一篇
- React 入门总结