HDU 4175 暴力求解

这道题题意那是一个晦涩难懂啊,,,看了好久才明白,看明白后一位是个动态规划,因为复杂度过高,后来浩哥试着写了个纯暴力的程序,竟然过了,而且才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 E ij 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 P ij. 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 ≤ E ij ≤ 1,000,000 
0 ≤ P ij ≤ 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代码:

  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <cstdio>  
  4. #include <math.h>  
  5.   
  6. using namespace std;  
  7.   
  8. struct point  
  9. {  
  10.     int pos,value;  
  11. }pp[27][1010];  
  12. int dp[27][1010];  
  13.   
  14. int main()  
  15. {  
  16.     //freopen("1.txt","r",stdin);  
  17.     int numcase;  
  18.     scanf("%d",&numcase);  
  19.     for(int k = 1;k <= numcase;++k)  
  20.     {  
  21.         int numl,numc,len,i,j,p;  
  22.         memset(dp,0,sizeof(dp));  
  23.         scanf("%d%d%d",&numl,&numc,&len);  
  24.         for(i = 1;i <= numl;++i)  
  25.         {  
  26.             for(j = 1;j <= numc;++j)  
  27.             {  
  28.                 scanf("%d%d",&pp[i][j].pos,&pp[i][j].value);  
  29.             }  
  30.         }  
  31.        int mmin = 200000000;  
  32.        for(i = 1;i <= numl;++i)  
  33.        {  
  34.            for(j = 1;j <= numc;++j)  
  35.            {  
  36.                mmin = 200000000;  
  37.                for(p = 1;p <= numc;++p)  
  38.                {  
  39.                    dp[i][j] = dp[i-1][p] + abs(pp[i][j].pos - pp[i-1][p].pos) + pp[i][j].value;  
  40.                    if(dp[i][j] < mmin)  
  41.                    {  
  42.                        mmin = dp[i][j];  
  43.                    }  
  44.                }  
  45.                dp[i][j] = mmin;  
  46.                //cout<<dp[i][j]<<endl;  
  47.              }  
  48.        }  
  49.        mmin = 200000000;  
  50.        for( i = 1;i <= numc;++i)  
  51.        {  
  52.            dp[numl][i] = dp[numl][i] + abs(len - pp[numl][i].pos);  
  53.            if(dp[numl][i] < mmin)  
  54.            {  
  55.                mmin = dp[numl][i];  
  56.            }  
  57.        }  
  58.        printf("%d\n",mmin);  
  59.     }  
  60.     return 0;  
  61. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值