UVA 1025 A Spy in the Metro DP水题
生活随笔
收集整理的這篇文章主要介紹了
UVA 1025 A Spy in the Metro DP水题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單DAG,每個狀態有三種決策,原地傻等,上往左開的車,上往右開的車。
#include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <bitset> #include <list> #include <cstdlib> #include <climits> #include <cmath> #include <ctime> #include <algorithm> #include <stack> #include <sstream> #include <numeric> #include <fstream> #include <functional>using namespace std;#define MP make_pair #define PB push_back typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef pair<int,int> pii; const int INF = INT_MAX / 3; const double eps = 1e-8; const LL LINF = 1e17; const double DINF = 1e60; const int maxn = 55; const int maxt = 400; int f[maxt][maxn], N, T; int cost[maxn], M1, M2, d1[maxn], d2[maxn]; bool left_bus[maxn][maxt], right_bus[maxn][maxt];int main() {int kase = 1;while(scanf("%d",&N), N) {memset(f,0x3f,sizeof(f));memset(cost,0,sizeof(cost));memset(left_bus,0,sizeof(left_bus));memset(right_bus,0,sizeof(right_bus));int inf = f[0][0];scanf("%d",&T);for(int i = 1;i < N;i++) scanf("%d",&cost[i]);scanf("%d",&M1);for(int i = 1;i <= M1;i++) scanf("%d",&d1[i]);scanf("%d",&M2);for(int i = 1;i <= M2;i++) scanf("%d",&d2[i]);for(int i = 1;i <= M1;i++) {int nowtime = d1[i];for(int j = 1;j <= N;j++) {left_bus[j][nowtime] = true;nowtime += cost[j];}}for(int i = 1;i <= M2;i++) {int nowtime = d2[i];for(int j = N;j >= 1;j--) {right_bus[j][nowtime] = true;nowtime += cost[j - 1];}}f[0][1] = 0;for(int i = 0;i <= T;i++) {for(int j = 1;j <= N;j++) {//waitf[i + 1][j] = min(f[i + 1][j],f[i][j] + 1);//go left_busif(right_bus[j][i] && j > 1 && i + cost[j - 1] <= T) {f[i + cost[j - 1]][j - 1] = min(f[i + cost[j - 1]][j - 1],f[i][j]);}//go right_busif(left_bus[j][i] && j < N && i + cost[j] <= T) {f[i + cost[j]][j + 1] = min(f[i + cost[j]][j + 1],f[i][j]);}}}printf("Case Number %d: ",kase++);if(f[T][N] == inf) puts("impossible");else printf("%d\n",f[T][N]);}return 0; }
轉載于:https://www.cnblogs.com/rolight/p/3966017.html
總結
以上是生活随笔為你收集整理的UVA 1025 A Spy in the Metro DP水题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Android] 修改ImageVie
- 下一篇: HFSS阵列天线设计与仿真2