zoukankan      html  css  js  c++  java
  • POJ 1018 Communication System (动态规划)

    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


    题解:我们定义状态dp 【i】【j】 表示选择了前 i 个宽带其容量为 j 的最小费用

    很容易得到转移方程 :dp【i】【j】=min(dp【i】【j】,dp【i-1】【k】+p);注意选择 j 的时候的大小情况
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 using namespace std;
    13 #define lowbit(x) (x&(-x))
    14 #define max(x,y) (x>y?x:y)
    15 #define min(x,y) (x<y?x:y)
    16 #define MAX 100000000000000000
    17 #define MOD 1000000007
    18 #define pi acos(-1.0)
    19 #define ei exp(1)
    20 #define PI 3.141592653589793238462
    21 #define INF 0x3f3f3f3f3f
    22 #define mem(a) (memset(a,0,sizeof(a)))
    23 typedef long long ll;
    24 ll gcd(ll a,ll b){
    25     return b?gcd(b,a%b):a;
    26 }
    27 bool cmp(int x,int y)
    28 {
    29     return x>y;
    30 }
    31 const int N=1005;
    32 const int mod=1e9+7;
    33 const int inf = 0x3f3f3f3f;
    34 int dp[120][1200];
    35 int main()
    36 {
    37     int t;
    38     scanf("%d",&t);
    39     while(T--){
    40         int n;
    41         scanf("%d",&n);
    42         for(int i=1;i<=n;i++){
    43             for(int j=0;j<1100;j++)
    44                 dp[i][j]=inf;
    45         }
    46         for(int i=1; i<=n; i++) {
    47             int num;
    48             scanf("%d",&num);
    49             for(int j=1; j<=num;j++){
    50                 int p,b;
    51                 scanf("%d%d",&b,&p);
    52                 if(i==1){
    53                     dp[1][b]=min(dp[1][b],p);
    54                 }
    55                 else{
    56                     for(int k=0;k<1100;k++){
    57                         if(dp[i-1][k]!=inf){
    58                             if(k<=b)
    59                                 dp[i][k]=min(dp[i][k],dp[i-1][k]+p);
    60                             else
    61                                 dp[i][b]=min(dp[i][b],dp[i-1][k]+p);
    62                         }
    63                     }
    64                 }
    65             }
    66         }
    67         double ans=0;
    68         for(int i=0;i<1100;i++){
    69             if(dp[n][i]!=inf){
    70                 double k=(double)i/dp[n][i];
    71                 if(k>ans)
    72                     ans=k;
    73             }
    74         }
    75         printf("%.3lf
    ",ans);
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    【leetcode】328 Odd Even Linked List
    【leetcode】86 partition list
    【leetcode】92 Reverse Linked List II
    【leetcode】220 ContainsDuplicate3
    【leetcode】219 Contains Duplicate2
    【leetcode】149 Max Points on a Line
    Java实现验证码图片
    求解两点(经纬度坐标)的距离
    POI开源项目-PPT2PNG转换测试
    编程内功修炼之设计模式—责任链模式
  • 原文地址:https://www.cnblogs.com/wydxry/p/7274517.html
Copyright © 2011-2022 走看看