ZOJ 1409 Communication System

这题用的是贪心算法,不过在贪心之前还是要进行部分处理的。

首先就是题目要求B/P尽可能的大,所以P应该尽可能的小,B应该尽可能的大。但是B和P的处理方式是不一样的,B是所有带宽中最小的那一个,P是所有设备的总价钱,所以我能想到一个方法就是一个一个的枚举B,本来我是不敢这样想的,可是题目给的时间比较长,我觉得应该问题不大,当我运行之后,竟然只是0ms,让我吃了一惊。然后再选择设备,这时候就要用到贪心:

给定一个band,对于一个设备,在各个生产厂家之间的选择,显然带宽要比band大,在这个中选择价钱最便宜的。

我的具体处理细节如下:

1、对所有的band进行升序排序,枚举的时候从最小的开始,当枚举到一个band,某一个设备无法选出,也就是说该设备的各个生产厂家的带宽都没有band大,那么就结束了。

2、对每个设备的band进行降序排序,这样在查找最小的price的时候比较方便。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int inf=1<<28;
int band[10002],num[102];
struct Device
{
    int band;
    int price;
}device[102][102];
int n,m;
int solve(int t)
{
    int t1=0,t2;
    for(int i=1;i<=n;i++)
    {
        t2=inf;
        for(int j=1;j<=num[i];j++)
        {
            if(device[i][j].band<t)
                break;
            if(t2>device[i][j].price)
                t2=device[i][j].price;
        }
        if(t2==inf)
            return -1;
        t1+=t2;
    }
    return t1;
}
bool cmp(Device a,Device b)
{
    return a.band>b.band;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int top=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&m);
            num[i]=m;
            for(int j=1;j<=m;j++)
            {
                scanf("%d%d",&device[i][j].band,&device[i][j].price);
                band[top++]=device[i][j].band;
            }
        }
        sort(band+1,band+top);
        for(int i=1;i<=n;i++)
            sort(device[i]+1,device[i]+num[i]+1,cmp);
        int t_band=0,sum;
        double ans=0.0;
        for(int i=1;i<top;i++)
        {
            if(band[i]==t_band)//如果相同的带宽已经处理过,那么就不用处理了
                continue;
            t_band=band[i];
            sum=solve(band[i]);
            if(sum==-1)
                break;
            double b_p=band[i]*1.0/sum;
            if(ans<b_p)
                ans=b_p;
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}
时间: 2024-08-02 06:02:16

ZOJ 1409 Communication System的相关文章

ZOJ 1409 communication system 双变量型的DP

这个题目一开始不知道如何下手,感觉很像背包,里面有两个变量,一个带宽B,一个价格P,有n个设备,每个设备有k个可选的器材(只需选一个),每个器材都有自己的B和P, n个设备选n个器材,最终,FB=所有器材里最小的B,FP=总的价格,要使得FB/FP最大 这种题目得先把一个变量给控制起来,或者说枚举其中一个变量之后,再通过dp的方法得到另一个变量的最优值,千万别想一步登天 像这个题目,我们如果先把每个设备对应的器材按价格升序排序,然后我枚举可能的FB,对于每个设备,我从前往后扫,一旦扫到了B>=F

POJ 1018 Communication System 题解

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

zoj1409 Communication System

[题解]: [代码]: 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #define inf 99999999 5 using namespace std; 6 int dp[105][10005];//i件设备,最小带宽(瓶颈)为j时的最小花费 7 int B[105][105]; 8 int P[105][105]; 9 int M[105],N; 10 double fmax(

poj1018 Communication System

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 d

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.

poj 1018 Communication System (枚举)

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

F - 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 the

POJ 1018 Communication System(DP)

http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产的同种设备都会存在两个方面的差别:带宽bandwidths 和 价格prices. 现在每种设备都各需要1个,考虑到性价比问题,要求所挑选出来的n件设备,要使得B/P最大. 其中B为这n件设备的带宽的最小值,P为这n件设备的总价. 思路:DP解决. d[i][j]代表选择第i个设备时最小带宽j时的价

贪心/poj 1018 Communication System

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int INF=0xfffffff; 6 struct node 7 { 8 int b,p; 9 }; 10 node a[110][110]; 11 int m[110]; 12 int pmin,psum,bmax,n; 13 double ans; 14 int main() 15