POJ 1018 (DP Or enumeration)

Source: Internet
Author: User
Tags cmath

Communication System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 23738 Accepted: 8437

Description

We have the received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we is free to choose from several manufacturers. Same devices from the 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 T Otal Price (P) was the sum of the prices of all chosen devices. Our goal are 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 is starts with a line containing a single integer n (1≤n≤100) and 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 O F manufacturers for the i-th device, followed by mi pairs of positive integers on the same line, each indicating the BANDW Idth 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 33 100 25 150 35 80 252 120 80 155 402 100 100 120 110

Sample Output

0.649

Source

Tehran 2002, first Iran Nationwide Internet programming Contest

[Submit]   [Go back]   [Status] [Discuss]

DP procedure, Dp[i][j] represents the minimum cost required for the minimum bandwidth of the first I item.

/************************************************************************* > File Name:xiaozhao.cpp > Autho R:acvcla > Qq:[email protected] > Mail: [email protected] > Created time:2014 December 27 Monday 22:34 13 sec *************************************************************************/#include <cstdio># include<iostream> #include <string> #include <cstring> #include <cmath> #include <algorithm > #include <queue> #include <cstdlib> #include <vector> #include <set> #include <map># Include<stack>using namespace Std;typedef long long ll;typedef pair<int,int>pii;const int maxn=1e2+10; const int Maxv=1e3+10;int dp[maxn][maxv],b[maxn],p[maxn];int Main () {int t,n,m;scanf ("%d", &t), while (t--) {scanf (" %d ", &n); memset (dp,0,sizeof dp); int maxb=0;for (int i=0;i<n;++i) {scanf ("%d ", &m); for (int j=0;j<m;++j) { scanf ("%d%d", &b[j],&p[j]); Maxb=max (Maxb,b[j]);} if (i==0) {for (int j=0;j<m;++J) {if (!dp[0][b[j]]) dp[0][b[j]]=p[j];else dp[0][b[j]]=min (Dp[0][b[j]],p[j]);} Continue;} for (int j=1;j<=maxb;j++) if (Dp[i-1][j]) {for (int k=0;k<m;k++) {int minb=min (B[K],J); if (!dp[i][minb]) dp[i][minb ]=dp[i-1][j]+p[k];else dp[i][minb]=min (Dp[i][minb],dp[i-1][j]+p[k]);}} Double ans=0;for (int i=1;i<=maxb;i++) {if (!dp[n-1][i]) Continue;ans=max (Ans,1.0*i/dp[n-1][i]);} printf ("%.3f\n", ans);} return 0;}

Enumeration actually faster than DP ... , enumerate the minimum bandwidth.


/************************************************************************* > File Name:xiaozhao.cpp > Autho R:acvcla > Qq:[email protected] > Mail: [email protected] > Created time:2014 December 27 Monday 22:34 13 sec *************************************************************************/#include <cstdio># include<iostream> #include <string> #include <cstring> #include <cmath> #include <algorithm > #include <queue> #include <cstdlib> #include <vector> #include <set> #include <map># Include<stack>using namespace Std;typedef long long ll;typedef pair<int,int>pii;const int maxn=1e4+10; const int inf=0x3f3f3f3f;vector<pii>communication[maxn];vector<int> v;double work (int x,int n) {int b=inf; Double p=0;for (int i=0;i<n;i++) {int ch=-1;for (int j=0;j<communication[i].size (); ++j) if (Communication[i][j]. FIRST&GT;=X) {if (ch==-1| | Communication[i][j].second<communication[i][ch].second|| (Communication[i][j].second==communication[i][ch].second&&communication[i][j].first>communication[i ][ch].first)) {ch=j;}} if (ch==-1) return-1.0;b=min (b,communication[i][ch].first);p +=communication[i][ch].second;} return b/p;} double solve (int n) {sort (V.begin (), V.end ()), Unique (V.begin (), V.end ()), int l=0,r=v.size ();d ouble ans=-3.0,p=0;for ( int i=0;i<v.size (); ++i) {p=work (v[i],n); if (p<=0) return Ans;ans=max (P,ans);} return ans;} int main () {int t,n,m;scanf ("%d", &t), while (t--) {scanf ("%d", &n), and for (int i=0;i<n;i++) communication[i]. Clear (); V.clear (); for (int i=0;i<n;i++) {scanf ("%d", &m), int b,p;while (m--) {scanf ("%d%d", &b,&p); V.push_back (b); Communication[i].push_back (Make_pair (B,p));}} printf ("%.3f\n", Solve (n));} return 0;}



POJ 1018 (DP Or enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.