Communication System(动态规划)

个人心得:百度推荐的简单DP题,自己做了下发现真得水,看了题解发现他们的思维真得比我好太多太多,

这是一段漫长的锻炼路呀。

关于这道题,我最开始用DP的思路,找子状态,发现自己根本就不会找DP状态数组建立,怎么找都是被后面的给打乱了,

看到了网上的DP【i】【j】,i表示前几个设备,宽带为j的最少花费,哇,真得厉害,突然发现动态规划并不需要做到问题很完美,

其实只要解决方案能够顺带把这个解决就好了。

他的这个转移方程就是,当i等于1时,输入的想,输入的x,y(x表示宽带长度,y表示价格)dp【i】【x】=y;

当i往后面递推时,如果此时i-1中k宽带存在的话,就跟此时的比较,如果此时的x小于K的话就可以直接放进去,大于的话就新的dp【i】【x】=min(dp【i】【x】,dp【i-1】【k】+y);

虽然此时的并不一定会是最优解,但是一步一步递推就把前面所有的情况都包含了进去,就可以一步一步得到前n个各个最小最大宽带数的最小价格;

动态规划的核心永远在状态的寻找,和转移方程的建立,看这里

 1  for(int i=1;i<=n;i++){
 2         int m;
 3         scanf("%d",&m);
 4         for(int j=1;j<=m;j++){
 5             int x,y;
 6                scanf("%d%d",&x,&y);
 7         if(i==1){
 8             dp[i][x]=y;
 9         }
10         else
11         {
12             for(int k=0;k<1500;k++)
13                 if(dp[i-1][k]!=inf)
14             {
15                 if(k<=x)
16                     dp[i][k]=min(dp[i][k],dp[i-1][k]+y);
17                 else
18                     dp[i][x]=min(dp[i][x],dp[i-1][k]+y);
19             }
20 
21         }
22     }

Description

We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers differ in their maximum bandwidths and prices. 
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P. 

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case starts with a line containing a single integer n (1 ≤ n ≤ 100), the number of devices in the communication system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device, followed by mi pairs of positive integers in the same line, each indicating the bandwidth and the price of the device respectively, corresponding to a manufacturer.

Output

Your program should produce a single line for each test case containing a single number which is the maximum possible B/P for the test case. Round the numbers in the output to 3 digits after decimal point. 

Sample Input

1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110

Sample Output

0.649
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int dp[105][1501];
 6 const int inf=99999;
 7 double mina(double x,double y){
 8     return x<y?x:y;
 9 }
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14      int n;
15      scanf("%d",&n);
16      for(int i=1;i<=n;i++)
17         for(int j=1;j<1500;j++)
18            dp[i][j]=inf;
19     for(int i=1;i<=n;i++){
20         int m;
21         scanf("%d",&m);
22         for(int j=1;j<=m;j++){
23             int x,y;
24                scanf("%d%d",&x,&y);
25         if(i==1){
26             dp[i][x]=y;
27         }
28         else
29         {
30             for(int k=0;k<1500;k++)
31                 if(dp[i-1][k]!=inf)
32             {
33                 if(k<=x)
34                     dp[i][k]=min(dp[i][k],dp[i-1][k]+y);
35                 else
36                     dp[i][x]=min(dp[i][x],dp[i-1][k]+y);
37             }
38 
39         }
40     }
41     }
42     double ans=0;
43     for(int j=1;j<1500;j++)
44     {
45         if(dp[n][j]!=inf)
46         {
47             double t=(double)j/dp[n][j];
48             if(t>ans)
49                 ans=t;
50     }
51     }
52     printf("%.3f\n",ans);
53     }
54      return 0;
55 }

 



 

posted @ 2017-08-22 16:50  余生漫漫浪  阅读(431)  评论(0编辑  收藏  举报