hdu 2437 (记忆化搜索)

Jerboas

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1256    Accepted Submission(s): 329


Problem Description
      Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long hind legs. Jerboas shelter in well-hidden burrows. They create two types of burrow: temporary and permanent. The temporary burrows are plain tubes while the permanent burrows are sealed with a plug of sand to keep heat out and moisture in. 

hdu 2437 (记忆化搜索)_第1张图片

      As far as we know, jerboa burrows in the desert are connected with one-way tunnels. What's more, for some unknown reasons, it's true that start from any burrow, follows the tunnels you can not go back to the starting burrow.
      Summer means last-minute of offers on good times, so of course jerboas could not stay behind. One day, a little jerboa Alice who lived in a temporary burrow S wants to migrate to a permanent one. There are different routes she can take, but Alice is so odd that she only selects those whose total travel distances is a multiple of K. Among all routes that Alice may select, we are interested in the shortest one. Can you help to find it out? Of course different routes may lead to different destinations.
 

Input
      On the first line of input, there is a single positive integer T <= 20 specifying the number of test cases to follow. 
      Each test case starts with four integers in the first line: N, M, S, K. 
      N is the number of burrows in the desert (burrows are numbered with 1, 2, …, N); 
      M is the number of tunnels connecting the burrows; 
      S is where Alice lived and K is as described above. 
(0 < N <= 1000, 0 <= M <= 20000, 0 < S <= N, 0 < K <= 1000)
      The second line contains N characters each could be ‘T’ or ‘P’. The i-th character specifying the type of the burrow i. ‘T’ means temporary burrow, ‘P’ means permanent burrow. It’s guaranteed that the S-th character is ‘T’.
      Next follow M lines, each line with 3 integers A, B, C. Specifying that there is a tunnel from burrow A to burrow B, and its length is C. 
(0 < A, B <= N, A != B, 0 < C < 40000)
 

Output
      For each test case you should output a single line containing "Case X: Y Z" (quotes for clarity) where X is the number of the test case (starting at 1) and Y is the length of the shortest route Alice can select and Z is the destination of the selected route.
      Notice that burrow Z should be a permanent burrow.
      In case there’s more than one solution, Z should be the minimum. 
      In case there's no solution, Y and Z should be both equal to -1.
 

Sample Input
   
   
   
   
2 5 5 1 7 TPPTP 1 2 8 1 4 7 4 3 9 2 3 6 1 5 3 5 5 1 7 TPTTP 1 2 8 1 4 7 4 3 9 2 3 6 1 5 3
 

Sample Output
   
   
   
   
Case 1: 14 3 Case 2: -1 -1
 

Source
2008 Asia Chengdu Regional Contest Online
 

Recommend
lcy
 
Dag图上的dp,要求起点到终点的距离是k的倍数且最短。容易想到dp[i][j]表示起点到i点距离模k为j的最短距离。这样状态是n^2的,而如果要把这个表打满,每次转移的代价都是O(n)的,所以肯定不能完全打表,即倒着推是不行的。怎么办,这道题可以正着推,根据当前点所在位置,和它相邻的边,推出它能实际意义到达的后继节点的状态。这里就需要一个重要的剪枝,如果后继的那个状态没有被更新,就在这儿剪枝。还有一个剪枝,当走到一个满足条件的终点时,就剪枝,因为后面再走下去肯定也不是最优的了。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include<vector>
using namespace std;
const int maxn = 1000 + 5;
const int maxm = 20000 + 5;
const int INF = 1000000000;

struct Edge{
    int to,dis;
    Edge(){};
    Edge(int to,int dis){
        this -> to = to;
        this -> dis = dis;
    }
}e[maxm];
vector<int> G[maxn];
int n,k;
char kind[maxn];
int dp[maxn][maxn];
int ans[maxn];

int dfs(int x,int cur){
    if(kind[x] == 'P' && cur%k == 0) return ans[x] = min(ans[x],cur);
    for(int i = 0;i < G[x].size();i++){
        int to = e[G[x][i]].to;
        int dis = e[G[x][i]].dis + cur;
        if(dp[to][cur%k] == -1 || dp[to][cur%k] > dis){
            dp[to][cur%k] = dis;
            dfs(to,dis);
        }
    }
}

int main(){
    int m,s;
    int t,kase = 0;
    scanf("%d",&t);
    while(t--){
        kase++;
        scanf("%d%d%d%d",&n,&m,&s,&k);
        scanf("%s",kind+1);
        for(int i = 0;i < maxn;i++) G[i].clear();
        for(int i = 0;i < m;i++){
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            e[i] = Edge(y,c);
            G[x].push_back(i);
        }
        memset(dp,-1,sizeof(dp));
        for(int i = 0;i < maxn;i++) ans[i] = INF;
        dfs(s,0);
        int ansx = INF,ansid;
        for(int i = 1;i <= n;i++){
            if(ans[i] < ansx){
                ansx = ans[i];
                ansid = i;
            }
        }
        if(ansx == INF){
            printf("Case %d: -1 -1\n",kase);
        }
        else printf("Case %d: %d %d\n",kase,ansx,ansid);
    }
    return 0;
}


你可能感兴趣的:(hdu 2437 (记忆化搜索))