HDU 4175 Class Schedule (暴力+一点dp)
2015-07-26 23:32
330 查看
HDU 4175
题意:有C座楼,每座楼有T个教室,一个人需要访问C个教室,每座楼只能访问一个教室。访问教室需要消耗能量,从x点走到y点需要消耗abs(x-y)的能量,最后要走到目的点L,问最后走到目的点L需要消耗的最少能量。
思路:读清题意,用getchar()的方式去读。。此外英文阅读水平比较拙计,亟待提升,以后不能再直接用有道翻译来做题了!
直接暴力枚举。用dp[i][j]表示class = i , classroom = j的所需最小能量,
直接看代码吧:)
code:
题意:有C座楼,每座楼有T个教室,一个人需要访问C个教室,每座楼只能访问一个教室。访问教室需要消耗能量,从x点走到y点需要消耗abs(x-y)的能量,最后要走到目的点L,问最后走到目的点L需要消耗的最少能量。
思路:读清题意,用getchar()的方式去读。。此外英文阅读水平比较拙计,亟待提升,以后不能再直接用有道翻译来做题了!
直接暴力枚举。用dp[i][j]表示class = i , classroom = j的所需最小能量,
dp[i][j] = dp[i-1][k] + abs(e[i-1][k].pos - e[i][j].pos) + e[i][j].cost;
直接看代码吧:)
code:
/* * @author Novicer * language : C++/C */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> #define inf 20000000 using namespace std; const double eps(1e-8); typedef long long lint; struct cl{ int pos; int cost; }; cl e[30][1005]; int dp[30][1005];//dp[i][j]表示class = i , classroom = j的所需最小能量 int main(){ // freopen("input.txt","r",stdin); int kase; cin >> kase; while(kase--){ // memset(e,0,sizeof(e)); memset(dp,0,sizeof(dp)); int c,t,l; cin >> c >> t >> l; for(int i = 1 ; i <= c ; i++){ for(int j = 1 ; j <= t ; j++){ scanf("%d%d",&e[i][j].pos,&e[i][j].cost); } } for(int i = 1 ; i <= c ; i++){ for(int j = 1 ; j <= t ; j++){ int tmp = inf; for(int k = 1 ; k <= t ; k++){ dp[i][j] = dp[i-1][k] + abs(e[i-1][k].pos - e[i][j].pos) + e[i][j].cost; tmp = min(dp[i][j] , tmp); } dp[i][j] = tmp; // cout << dp[i][j] << endl; } } int ans = inf; for(int i = 1 ; i <= t ; i++){ int value = dp[c][i] + abs(l - e[c][i].pos); ans = min(ans , value); } cout << ans << endl; } return 0; }
相关文章推荐
- linux程序设计——select调用和多客户(第十五章)
- 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)
- 常见的块状元素与内联元素
- SQL面试题
- 学习笔记:linuxc第六章下
- POJ 3071 Football(概率DP)
- Linux下信号的基本使用与分析
- form表单按enter键自动提交的问题
- 兼容问题及处理
- C# 对于时间的相关问题
- java 常用 Java 静态代码分析工具的分析与比较
- moodle的一些api翻译~$page翻译
- 通过Cloudera Manager为datanode增加数据目录
- component to string
- 向PE中注入代码
- 交换a、b的值temp = a; a = b; b = temp;比a = a^b;b = a^b;a = a^b;快
- 广搜状态压缩 OpenJudge 百炼4105
- poj 2392 Space Elevator
- struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题
- hdu4101