看完题目的开头,大概是个二维spfa,产生了此题可搞的幻觉。。。
接着就看到了这句“But the matter is not so simple.”,次奥,看来是个三维的呢。。。
接着就看到了这句“However, the problem is more complicated than imagine. ”,尼玛四维。。。好吧,四维spfa本质还是spfa吧。。
于是就出现了在hdu各种TLE的悲剧。。无奈看了看discuss,大家的spfa貌似都T了呢。。。
接着就有流言,说是优先队列bfs能过。。。终于输给了现实,换了个优先队列的才过。。。
注意题目中的“ will end his journey immediately once he arrives at Uncle Yang’s house. ”这句,到达n之后就不能松弛了。
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #define REP(i, n) for(int i=0; i<n; i++) #define FF(i, a, b) for(int i=a; i<b; i++) #define CLR(a, b) memset(a, b, sizeof(a)) #define PB push_back using namespace std; int C, N, M, B, K, R, T; int price[10][111], dp[111][222][10][10]; bool inq[111][222][10][10]; struct Edge { int to, dist, cost; Edge(int a=0, int b=0, int c=0) : to(a), dist(b), cost(c){} }; vector<int> G[111]; vector<Edge> edges; struct Node { int u, t, b, k; Node(int u=0, int t=0, int b=0, int k=0) : u(u), t(t), b(b), k(k){} bool operator < (const Node& rhs) const { return t > rhs.t; } }; priority_queue<Node> q; void init() { CLR(dp, -1); CLR(inq, 0); REP(i, N+1) G[i].clear(); edges.clear(); } void relax(int u, int t, int b, int k, int w) { if((u == 1 || u == N) && k != 0) return ; if(dp[u][t][b][k] < w) { dp[u][t][b][k] = w; if(!inq[u][t][b][k]) { q.push(Node(u, t, b, k)); inq[u][t][b][k] = true; } } } void spfa() { dp[1][0][0][0] = R; inq[1][0][0][0] = true; q.push(Node(1, 0, 0, 0)); while(!q.empty()) { Node tmp = q.top(); q.pop(); int u=tmp.u, t=tmp.t, b=tmp.b, k=tmp.k, w=dp[u][t][b][k]; inq[u][t][b][k] = false; int kk = (k + 1) % K; if(t < T) { relax(u, t+1, b, kk, w); if(price[k][u] != -1) { if(b > 0) relax(u, t+1, b-1, kk, w + price[k][u]); if(b < B && w >= price[k][u]) relax(u, t+1, b+1, kk, w - price[k][u]); } } REP(i, G[u].size()) { if(u == N) break; Edge e = edges[G[u][i]]; int v = e.to, dist = e.dist, cost = e.cost; if(t + dist > T || w < cost) continue; relax(v, t + dist, b, k, w - cost); if(price[k][u] != -1) { if(b > 0) relax(v, t+dist, b-1, k, w-cost+price[k][u]); if(b < B && w >= price[k][u]+cost) relax(v, t+dist, b+1, k, w-cost-price[k][u]); } } } } int main() { scanf("%d", &C); FF(kase, 1, C+1) { init(); scanf("%d%d%d%d%d%d", &N, &M, &B, &K, &R, &T); REP(i, K) FF(j, 1, N+1) scanf("%d", &price[i][j]); int sz = 0; while(M--) { int a, b, t, m; scanf("%d%d%d%d", &a, &b, &t, &m); edges.PB(Edge(b, t, m)); G[a].PB(sz++); } spfa(); int ans = 0, flag = 0; REP(i, T+1) if(dp[N][i][0][0] != -1) { flag = 1; ans = max(ans, dp[N][i][0][0]); } printf("Case #%d: ", kase); if(flag) printf("%d\n", ans); else puts("Forever Alone"); } return 0; }