代码改变世界

HDU 4175 暴力求解

2012-05-07 19:48  javaspring  阅读(214)  评论(0编辑  收藏  举报

         这道题题意那是一个晦涩难懂啊,,,看了好久才明白,看明白后一位是个动态规划,因为复杂度过高,后来浩哥试着写了个纯暴力的程序,竟然过了,而且才42ms,,我去,这道题数据得有多水。。。。。。

     题意:有C座楼,每座楼有T个教室,一个人需要访问C个教室,每座楼只能访问一个教室。访问教室需要消耗能量,从x点走到y点需要消耗abs(x-y)的能量,最后要走到目的点L,问最后走到目的点L需要消耗的最少能量。

    思路:开始想着用动态规划,和数塔非常的像,没想到暴力竟然可以过。就是求出到每座楼每个教室所需的最少能量即可。

题目:

Class Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 55    Accepted Submission(s): 29


Problem Description
At Fred Hacker's school, there are T × C classes, divided into C catagories of T classes each. The day begins with all the category 1 classes being taught simultaneously. These all end at the same time, and then all the category 2 classes are taught, etc. Fred has to take exactly one class in each category. His goal is to choose the set of classes that will minimize the amount of "energy'' required to carry out his daily schedule. 
The energy requirement of a schedule is the sum of the energy requirement of the classes themselves, and energy consumed by moving from one class to the next through the schedule. 

More specifically, taking the jth class in the ith category uses Eij units of energy. The rooms where classes take place are located at integer positions (ranging from 0 to L) along a single hallway. The jth class in the ith category is located at position Pij. Fred starts the day at position 0, moves from class to class, according to his chosen schedule, and finally exits at location L. Moving a distance d uses d units of energy. 
 

Input
The first line of the input is Z ≤ 20 the number of test cases. This is followed by Z test cases. Each test case begins with three space-separated integers: C, T, and L. Each of the following C× T lines gives, respectively, the location and energy consumption of a class. The first T lines represent the classes of category 1, the next T lines represent the classes of category 2, and so on. No two classes in the same category will have the same location. 

Bounds:
1 ≤ C ≤ 25 
1 ≤ T ≤ 1000 
1 ≤ L ≤ 1,000,000 
1 ≤ Eij ≤ 1,000,000 
0 ≤ Pij ≤ L
 

Output
For each input instance, the output will be a single integer on a line by itself which is the minimum possible energy of a schedule satisfying the constraints. 
 

Sample Input
1 3 2 5 2 1 3 1 4 1 1 3 1 4 3 2
 

Sample Output
11
Hint
Explanation of Sample Input: Fred must take 3 classes every day, and for each he has 2 choices. The hall has length 5. His first possible class is located at position 2 and will take 1 unit of energy each day, etc. Explanation of Sample Output: Here is one way to obtain the minimum energy: Go to the class at location 2. Energy used: 3 Next, go to the class at location 4. Energy used: 6 Then go to the class at location 3. Energy used: 9 Finally, leave the school at location 5. Energy used: 11
 

ac代码:

#include <iostream>
#include <string.h>
#include <cstdio>
#include <math.h>

using namespace std;

struct point
{
    int pos,value;
}pp[27][1010];
int dp[27][1010];

int main()
{
    //freopen("1.txt","r",stdin);
    int numcase;
    scanf("%d",&numcase);
    for(int k = 1;k <= numcase;++k)
    {
        int numl,numc,len,i,j,p;
        memset(dp,0,sizeof(dp));
        scanf("%d%d%d",&numl,&numc,&len);
        for(i = 1;i <= numl;++i)
        {
            for(j = 1;j <= numc;++j)
            {
                scanf("%d%d",&pp[i][j].pos,&pp[i][j].value);
            }
        }
       int mmin = 200000000;
       for(i = 1;i <= numl;++i)
       {
           for(j = 1;j <= numc;++j)
           {
               mmin = 200000000;
               for(p = 1;p <= numc;++p)
               {
                   dp[i][j] = dp[i-1][p] + abs(pp[i][j].pos - pp[i-1][p].pos) + pp[i][j].value;
                   if(dp[i][j] < mmin)
                   {
                       mmin = dp[i][j];
                   }
               }
               dp[i][j] = mmin;
               //cout<<dp[i][j]<<endl;
             }
       }
       mmin = 200000000;
       for( i = 1;i <= numc;++i)
       {
           dp[numl][i] = dp[numl][i] + abs(len - pp[numl][i].pos);
           if(dp[numl][i] < mmin)
           {
               mmin = dp[numl][i];
           }
       }
       printf("%d\n",mmin);
    }
    return 0;
}