poj1018 Communication System

dp简单题

#include 
#include  
using namespace std;
const int inf = 0x3f3f3f3f;
int dp[105][1200];

int main(){
    int t,n,r,band,cost;
    scanf("%d",&t);
    for (int i = 0; i < t; ++i)
    {
        for(int k = 0;k<105;k++){
            for (int t = 0; t < 1200; ++t)
                dp[k][t] = inf;
        }
        scanf("%d",&n);
        for (int j = 0; j < n; ++j)
        {
            scanf("%d",&r);
            for(int o=0;oscanf("%d%d",&band,&cost);
            if(j==0){
                dp[j][band] = min(dp[j][band],cost);
            }
            else{
                for(int k = 0;k<1200;k++){
                    if(dp[j-1][k]!=inf){
                    if(k<=band)
                        dp[j][k] = min(dp[j][k],dp[j-1][k]+cost);
                    else
                        dp[j][band] = min(dp[j][band],dp[j-1][k]+cost);
                    }
                }
            }
            }
        }
        double ans = 0;
        for(int i =0;i<1200;i++){
            if(dp[n-1][i]!=inf){
                double k = (double)i/dp[n-1][i];
                if(k>ans)
                    ans = k;
            }
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}

你可能感兴趣的:(poj1018 Communication System)