poj 1018(dp)

Communication System

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25653   Accepted: 9147

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

思路:定义dp[i][j]为前i个设备的容量为j的最小费用;    状态转移方程为:dp[i][j]=min(dp[i][j],dp[i-1][j]+p);    边界:dp[1][j]=p;      

  
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<iomanip>
 7 #include<cmath>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 using namespace std;
12 #define PI 3.141592653589792128462643383279502
13 const int inf=0x3f3f3f3f;
14 int t,n,j,ss,m[120][1200];
15 int main(){
16     //#ifdef CDZSC_June
17     //freopen("in.txt","r",stdin);
18     //#endif
19     //std::ios::sync_with_stdio(false);
20     scanf("%d",&t);
21     while(t--){
22         scanf("%d",&n);
23         memset(m,0x3f,sizeof(m));
24         for(int i=1;i<=n;i++){
25             int num;
26             scanf("%d",&num);
27             for(j=1;j<=num;j++){
28                 int b,p;
29                 scanf("%d%d",&b,&p);
30                 if(i==1){m[1][b]=min(m[1][b],p);}
31                 else {
32                     for(int k=0;k<1200;k++){
33                         if(m[i-1][k]!=inf){
34                             if(k<=b)
35                                 m[i][k]=min(m[i][k],m[i-1][k]+p);
36                             else
37                                 m[i][b]=min(m[i][b],m[i-1][k]+p);
38                         }
39                     }
40                 }
41             }
42         }
43     double ans=0;
44         for(int i=0;i<1200;i++){
45             if(m[n][i]!=inf){
46                 double k=(double)i/m[n][i];
47                 if(k>ans) ans=k;
48             }
49         }
50         printf("%.3lf\n",ans);
51     }
52     return 0;
53 }
时间: 2024-10-14 23:35:48

poj 1018(dp)的相关文章

POJ 1018(dp Or 枚举)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23738   Accepted: 8437 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

POJ 1018 Communication System 题解

本题一看似乎是递归回溯剪枝的方法,我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了很久,无奈忍不住偷看了下提示,发现方法真多,有贪心,DP,有高级剪枝的,还有三分法的,八仙过海各显神通啊. 坏习惯了,没思考够深入就偷看提示了. 幸好及时回头,还不需要看别人的代码了.自己做出来之后,有空看看多种解法的代码也好. 然后我想出自己的思路了,使用贪心,剪枝,DP综合优化下,呵呵,最后程序有点复杂,优化到了16ms,运气好点,或者vector换成原始数组的话,应该可以0MS了. 总体思

POJ 1018 Communication System (动态规划)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22500   Accepted: 8008 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

POJ 3670 &amp;&amp; POJ 3671 (dp)

最长不下降子序列的应用嘛.两题都是一样的. POJ 3670:求给定序列按递增或递减排列时,所需改变的最小的数字的数目. POJ 3671:求给定序列按递增排列时,所需改变的最小的数字的数目. 思路就是求最长不下降子序列,然后剩下的就是需要改变的字母. 最长不下降子序列:(我之前有写过,不懂请戳)http://blog.csdn.net/darwin_/article/details/38360997 POJ 3670: #include<cstdio> #include<cstring

poj 3783 DP 2个鸡蛋扔100层楼的加强版

http://poj.org/problem?id=3783 估计23号之后的排位赛之后我就要退役了,这之前最后再做5天ACM 今天的排位很惨,上次排位也很惨......这道题原来算法课老师讲过,模模糊糊记得方程,但是边界处理有问题, dp[i][j]=min(1+max(dp[k-1][j-1],dp[i-k][j]))   k=1 to 楼数 dp[i][j]:i层楼扔,手里有j个ball 的次数 边界两个:1.dp[1][i]=1,第一层无论手里有几个鸡蛋都是1次,2.dp[i][1]=i

POJ 3034 DP

打地鼠游戏中,你有一个锤子,每一秒钟你可以拿着锤子移动d个单位的距离,必须是直线,掠过的鼠洞中露出的地鼠都会被锤打至,而事先知道从开始时各时间段内出现在老鼠的数量和位置,问题是从游戏开始至结束时,你最多能打到多少只地鼠,开始时锤子可以在任何位置. 题目有个陷阱, 只是说Moles出现的坐标为正,但没说hammer移动的位置要为正,可以为"any position" 所以锤子可以移出矩阵,再从矩阵外一点移进来 例: 20 5 4 1 0 1 0 1 1 0 5 2 1 6 2 0 0 0

POJ 4968 DP||记忆化搜索

给出N个人的平局分X 根据GPA规则计算可能的最高平均GPA和最低平均GPA 可以DP预处理出来所有结果  或者记忆化搜索 DP: #include "stdio.h" #include "string.h" int inf=100000000; double a[11][1100],b[11][1100]; double Max(double a,double b) { if (a<b) return b; else return a; } double M

POJ 2029 DP || 暴力

在大矩形中找一个小矩形 使小矩形包含的*最多 暴力或者DP  水题 暴力: #include "stdio.h" #include "string.h" int main() { int n,m,w,i,s,t,j,k,l,ans,sum,x,y; int map[101][101]; while (scanf("%d",&w)!=EOF) { if(w==0) break; scanf("%d%d",&n,&