您的位置:首页 > 其它

Fixing the Great Wall UVA - 1336

2017-09-23 21:32 302 查看
简单的题目,按照紫书上面的思路即可,但是在实现的过程当中又感觉到了一丝丝贪心算法的味道,嗯,有一定的违和感......

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

struct location{
int x, weight, dt;
};

int n, x;
double v;
vector<location> loc;
double dp[1010][1010][2];
int sum[1010];
int start;
int ans;

bool compare(const location& a, const location& b){
return a.x < b.x;
}

void solve(){
for (int i = start; i >= 0; i--){
for (int j = i; j < n; j++){
dp[i][j][0] = dp[i][j][1] = 1 << 30;
}
}
dp[start][start][0] = dp[start][start][1] = 0;
for (int i = start; i >= 0; i--){
for (int j = start; j < n; j++){
double temp_s = (i == 0 ? 0 : sum[i - 1]) + sum[n-1] - sum[j];
if (i > 0){//dp[i-1][j][0]
double temp = dp[i][j][0] + ((loc[i].x - loc[i - 1].x) / v)*temp_s+loc[i-1].weight;
dp[i - 1][j][0] = min(dp[i - 1][j][0], temp);
temp = dp[i][j][1] + ((loc[j].x - loc[i - 1].x) / v)*temp_s+loc[i-1].weight;
dp[i - 1][j][0] = min(dp[i - 1][j][0], temp);
}
if (j < n-1){//dp[i][j+1][1]
double temp = dp[i][j][1] + ((loc[j + 1].x - loc[j].x) / v)*temp_s+loc[j+1].weight;
dp[i][j + 1][1] = min(dp[i][j + 1][1], temp);
temp = dp[i][j][0] + ((loc[j + 1].x - loc[i].x) / v)*temp_s+loc[j+1].weight;
dp[i][j + 1][1] = min(dp[i][j + 1][1], temp);
}
}
}
ans = min(dp[0][n-1][0],dp[0][n-1][1]);
}

int main(){
while (cin >> n >> v >> x){
if (n == 0 && v == 0 && x == 0) break;
ans = 1 << 30;
loc.clear();
for (int i = 0; i < n; i++){
location temp;
cin >> temp.x >> temp.weight >> temp.dt;
loc.push_back(temp);
}
location t;
t.x = x, t.dt = 0, t.weight = 0;
loc.push_back(t);
sort(loc.begin(),loc.end(),compare);
n = loc.size();
for (int i = 0; i < loc.size(); i++){
if (loc[i].x==x){
start = i;
}
if (i == 0){
sum[i] = loc[i].dt;
}
else{
sum[i] = sum[i - 1] + loc[i].dt;
}
}
solve();
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: