POJ 2355 Railway Ticket problem
http://poj.org/problem?id=2355
要點:
- 插點更新的思路
- dp的初態設定要重視
- dp的更新順序有時是不影響的。
一維插點求單源最短路(?
- 在s與e間進行查詢
- 如果中間經停一個車站可以一次到達終點的話,就嘗試用到達這個經停加上最後一站的費用進行更新。
while迴圈尋找可以一步到達本次終點的節點。
for (int i = s+1; i <= e; i++)
{
int j = s;
while (dist[i] - dist[j] > l[3]) j++;
for (; j < i; j++)
dp[i] = min(dp[j] + cost(dist[i]-dist[j]), dp[i]);
}
最初以為這裡涉及二維變一維導致的覆蓋順序問題。但把兩段程式碼放在一起說明等價時,發現前一個while迴圈中的l[3]打成了c[3]。低階錯誤
但實際上這步操作可以利用倒序實現:
for (int i = s+1; i <= e; i++)
{
for (int j = i-1; j >= s; j--)
if (dist[i]-dist[j] > l[3])
break;
else
dp[i] = min(dp[j] + cost(dist[i]-dist[j]), dp[i]);
}
另外題中需要注意兩個初態:動態規劃的初始狀態一定要想明白!
距離的初態為0,dp初態為0。
#include <bits/stdc++.h>
using namespace std;
int l[4], c[4];
int cost(int km)
{
if (km <= l[1])
return c[1];
else if (km <= l[2])
return c[2];
else if (km <= l[3])
return c[3];
else return 1e9+100;
}
int main()
{
#ifdef _LOC_
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
cin >> l[1] >> l[2] >> l[3] >> c[1] >> c[2] >> c[3];
int n, s, e;
cin >> n >> s >> e;
if (s > e) swap(s, e);
int dist[10020];
int dp[10020]; memset(dp, 0x3f, sizeof dp);
dp[s] = 0; dist[1] = 0; //初態
for (int i = 2; i <= n; i++)
cin >> dist[i];
for (int i = s+1; i <= e; i++)
{
for (int j = i-1; j >= s; j--)
if (dist[i]-dist[j] > l[3])
break;
else
dp[i] = min(dp[j] + cost(dist[i]-dist[j]), dp[i]);
}
cout << dp[e];
#ifdef _LOC_
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
相關文章
- 【poj3468】A Simple Problem with Integers
- An Easy Problem?! POJ 2826 計算幾何
- (poj3468)A Simple Problem with Integers(區間更新)
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- A. Rudolf and the Ticket
- B - Ticket Counter
- POJ 2048 Longge's problem (尤拉函式 積性函式)函式
- POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)陣列
- CAS ticket過期策略
- C. Torn Lucky Ticket
- 設計Ticket Master (附解答)AST
- bd_ticket_guard_client_dataclient
- Problem_2 Majority Problem
- 使用RailWay部署Halo CMS部落格系統AI
- Kerberos ticket lifetime及其它ROS
- Bd-Ticket-Guard-Client-Data逆向client
- Mathematical Problem
- BZOJ2080 : [Poi2010]RailwayAI
- BZOJ4158 : [POI2007]RailwayAI
- FZU Problem 1692 Key problem(迴圈矩陣)矩陣
- ACM A problem is easyACM
- Database Transaction ProblemDatabase
- Yet Another Problem
- Fixed "There was a problem with the editor 'vi'"
- STL iterator delete problemdelete
- E. Not a Nim Problem
- HDU 1792 A New Change Problem
- Joe Harris is a real problem with this team
- The Door Problem 並查集並查集
- 3339: Rmq ProblemMQ
- ORB problem determinationORB
- Nanami and the Constructive ProblemNaNStruct
- POJ 3461 kmpKMP
- poj3417
- Assignment Problem的若干思考