题意:给出n种设备,每种有mi的制造商,每种设备有带宽和价钱两种属性,同种设备,不同制造商,这两种属性可能都不同。求一个选择制造商的方案,使带宽最小值(B)比总价钱(P)值最大。
按带宽求解,对于每个带宽值,求一个总价钱最小的方案。
代码如下:
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef struct device
- {
- vector <int> bands;
- vector <int> prices;
- int length()
- {
- return bands.size();
- }
- void clear()
- {
- bands.clear();
- prices.clear();
- }
- void add(int tmp_band, int tmp_price)
- {
- bands.push_back(tmp_band);
- prices.push_back(tmp_price);
- }
- }decive;
- int main()
- {
- int t, n, i, mi;
- int band_max, band_min;
- long sum;
- device devices[100];
- int k, j, tmp_band, tmp_price;
- double max_bdivp;
- cin >> t;
- while (t--)
- {
- cin >> n;
- band_max = 0;
- band_min = 32767;
- // input
- for (i = 0; i < n; ++i)
- {
- cin >> mi;
- devices[i].clear();
- for (k = 0; k < mi; ++k)
- {
- scanf("%d %d", &tmp_band, &tmp_price);
- devices[i].add(tmp_band, tmp_price);
- if (band_max < tmp_band)
- {
- band_max = tmp_band;
- }
- if (band_min > tmp_band)
- {
- band_min = tmp_band;
- }
- }
- }
- max_bdivp = 0.0;
- // solve
- for(j = band_min; j <= band_max; ++j)
- {
- sum = 0;
- for(k = 0; k < n; ++k)
- {
- int tmp = 32767;
- for(i = 0; i < devices[k].length(); i++)
- {
- if(devices[k].bands[i] >= j
- && devices[k].prices[i]< tmp)
- {
- tmp = devices[k].prices[i];
- }
- }
- sum += tmp;
- }
- if( j / (sum + 0.0) > max_bdivp)
- {
- max_bdivp = j / (sum + 0.0);
- }
- }
- // output
- printf("%.3lf/n", max_bdivp);
- }
- return 0;
- }
本文介绍了一种算法,用于从多种设备及其制造商中选取最优配置,目标是最小化总价格的同时最大化带宽。通过构建设备数据结构并遍历所有可能的带宽值来找到最佳的价格-带宽比率。
941

被折叠的 条评论
为什么被折叠?



