Poj1018 Communication System

                                                                                                                                      Communication System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20836 Accepted: 7381

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

     题目的意思就是说输入几个a[i][j].d,a[i][j].p,然后找出最到的d/p的值
     感觉第一行输入的3 第二行输入的3 2 2 
     每一行中都要选出一个
     


方法一: 贪心算法,枚举

           将a[i][j].b从小到大排序,然后从最小的开始遍历,找出每个i满足的条件之后算出一个d/p的值,然后i+1继续,最后求出最大值。
           样例都过了呀!怎么ac不了  = =

#include <iostream>
#include <cmath>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int Max=101;


struct node
{
    double b;
    double p;
};


node a[Max][Max];
double b[Max*Max];
int m[Max];
int bsize;


int cmp(const void *a,const void *b)
{
    return (*(double *)a)-(*(double *)b);
}


int main()
{
    int t,n;
    cin>>t;
    while (t--)
    {
        memset(a,0,sizeof(a));
        memset(m,0,sizeof(m));
        cin>>n;
        int i,j,k;
        int bsize=0;
        for (i=0;i<n;i++)
        {
            cin>>m[i];
            for (j=0;j<m[i];j++)
            {
                cin>>a[i][j].b>>a[i][j].p;
                b[bsize++]=a[i][j].b;
            }
        }
        qsort(b,bsize,sizeof(b[0]),cmp);
           for(i=0;i<bsize;i++)
           cout<<b[i]<<endl;
        double mmax=0;
        double mmin;
        double sump=0;
        double temp=0;
        for (i=b[0];i<=b[bsize-1];i++)
        {
            sump=0;
            for (j=0;j<n;j++)
            {
                mmin=32767;//这一步很重要,如果找不出符合条件的数据,就会变的很大。
                for (k=0;k<m[j];k++)
                {
                    if (a[j][k].b>=i&&a[j][k].p<mmin)
                    {
                        mmin=a[j][k].p;
                    }
                }
                sump+=mmin;//sump的值是每一行的符合条件的值组成的。
            }
            temp=i*1.0/sump;
               cout<<"i"<<i<<endl;
               cout<<"sump"<<sump<<endl;
               cout<<"temp"<<temp<<endl;
            if(temp>mmax)
                mmax=temp;
        }
        printf("%.3lf\n",mmax);


    }
    return 0;
}

方法二: 枚举的基础上加了个剪枝 (转)

解题思路:

首先需要明确,要使得B/P最大,自然是要令B尽可能大,P尽可能小。

由于B和P是两个相互制约的变量,而他们又要同时取得尽可能地取极值,那么可以先令其中一个值“暂时固定”下来。

令输入的行数就是设备的种类数,即第i行描述的是第i种设备,及提供第i种设备的厂家的产品信息。

 

使用枚举+剪枝的做法:

首先B的值肯定是厂家生产的所有设备中的某个带宽值,所以可以枚举所有的带宽,每次枚举B值时,B值就被“暂时固定”了。

其次,记录所选取的B是属于第k种设备的,再从余下的设备中,选取其余n-1种设备各一个,要求所选取的设备的带宽>=B(这是由题意确定的),而价格是要满足带宽的要求下的最小值。

当n种设备都选取后,计算B/P,然后再枚举下一个B,重复上述操作。比较不同的B值所得到的B/P值,选取最大的一个就是答案。

 

剪枝法:

准备工作:

1、输入时先记录:对于每种设备,厂家所提供的最大带宽MaxB[]

2、对所有设备(无论是否同种类)进行升序快排,以带宽为第一关键字,价格为第二关键字,设备所属的种类编号(1~n)为第三关键字。排序后存放在一维数组dev[]

剪枝:

1、  从小到大枚举dev[]中各个设备的带宽作为B值,设总设备数位m,则从1枚举到m-(n-1)。这是因为至少需要选择从枚举位置开始后面的n种设备,m-(n-1)是上限值,即恰好最后n件设备分别为n种设备。

2、  枚举B值的过程中,若发现B值比某种设备的最大带宽更大,则无需继续枚举,输出前面得到的B/P值。这是因为B是所有设备的最小带宽,若出现某个设备的最大带宽比B小,则说明B的选择是不合法的,又dev[]已按B升序排序,后面的B值更大,也不可能成立,因此无需继续枚举。

3、  枚举B值过程中,对于每个B值,在选择其他设备时要记录选取不同种类的设备个数count。最后当count<n时,说明B值位置往后剩余的设备中已无法提供n-1种不同设备,可直接跳出枚举。

 


剪枝2比较难懂,再稍微解释一下,以给的数据为例:
1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110

把该组数据升序排序,得到:

B:  80  100  100  120  120  150  155

P:  25  25   100   80  110   35   40

Id:  1   1     3    2    3    1    2


这样当B枚举到150的时候,即B=150,第三个供应商的所有设备都小于150,取任何一个设备都会导致B<150,矛盾。当然大于150的更不用枚举了,直接剪掉。

//Memory Time 
//1688K  188MS 

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;

class info
{
public:
	int B;   //带宽
	double P;   //价格
	int id;  //设备号码
};

int cmp(const void* a,const void* b)
{
	info* x=(info*)a;
	info* y=(info*)b;

	if((x->B)==(y->B))   //当带宽相等时
	{
		if((x->P)==(y->P))   //当价格也相等时
			return (x->id)-(y->id);   //以编号为第三优先升序排序

		return (x->P)-(y->P);   //以价格为第二优先升序排序
	}

	return (x->B)-(y->B);   //以带宽为第一优先升序排序
}

double max(double a,double b)
{
	return a>b?a:b;
}

int main(int i,int j)
{
	int test;
	cin>>test;
	for(int t=1;t<=test;t++)
	{
		int n;  //设备数
		int m=0;  //生产商总数
		cin>>n;

		int* MaxB=new int[n+1];  //各种设备对应的带宽最大值
		info* dev=new info[100*100+1];     //记录所有厂家生产的产品信息

		int pd=0;  //dev[]指针

		/*Input*/

		
		for(i=1;i<=n;i++)
		{
			int mi;
			cin>>mi;
			m+=mi;

			MaxB[i]=-1;
			for(j=1;j<=mi;j++)
			{
				pd++;
				cin>>dev[pd].B>>dev[pd].P;
				dev[pd].id=i;

				MaxB[i]=max(MaxB[i],dev[pd].B);
			}
		}

		/*Qsort*/

		qsort(dev,m+1,sizeof(info),cmp);

		/*Enum*/

		bool flag=false;
		double ans=0;  // B/P的最大值
		for(i=1;i<=m-(n-1);i++)  //枚举所有设备带宽的最小带宽B
		{                        //m-(n-1)是剪枝,因为当设备数>生产商数时就不必枚举了
			bool* vist=new bool[n+1];
			memset(vist,false,sizeof(bool)*(n+1));

			vist[ dev[i].id ]=true;
			double price=dev[i].P;  //设备总价
			int count=1;   //计数器,记录已经选取的设备个数

			for(j=i+1;j<=m;j++)
			{
				if(vist[ dev[j].id ])
					continue;

				if(dev[i].B > MaxB[ dev[j].id ])  //剪枝
				{
					flag=true;  //当前枚举的 "所有设备带宽的最小带宽Bi" 比 "设备j的最大带宽MaxBj" 要大
					break;      //说明当前Bi已经越界,无需继续往后枚举
				}

				vist[ dev[j].id ]=true;
				price+=dev[j].P;
				count++;
			}
			if(flag || count<n)
				break;

			ans=max(ans,(dev[i].B/price));
		}

		cout<<fixed<<setprecision(3)<<ans<<endl;

		delete MaxB;
		delete dev;
	}
	return 0;
}

方法三: 动态规划
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1<<28
using namespace std;
int p[105][1005];
int n,t,m;
int a,b,s;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<=100;i++)
        {
            for(int j=0;j<=1000;j++)p[i][j]=maxn;
        }
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&m);
            for(int j=0;j<m;j++)
            {
                scanf("%d%d",&b,&s);
                if(i==1)
                {
                    if(p[1][b]==maxn)p[1][b]=s;
                    else
                    {
                        if(p[1][b]>s)p[1][b]=s;
                    }
                }
                else
                {
                    for(int k=1;k<=1000;k++)
                    if(p[i-1][k]!=maxn)
                    {
                        if(k<b)
                        {
                            if(p[i][k]==maxn)p[i][k]=p[i-1][k]+s;
                            else if(p[i][k]>p[i-1][k]+s)p[i][k]=p[i-1][k]+s;
                        }
                        else
                        {
                            if(p[i][b]==maxn)p[i][b]=p[i-1][k]+s;
                            else if(p[i][b]>p[i-1][k]+s)p[i][b]=p[i-1][k]+s;
                        }
                    }
                }
            }
        }
        double ma=0;
        for(int i=1;i<=1000;i++)
        {
            ma=max(ma,(double)i/(double)p[n][i]);
        }
        printf("%.3f\n",ma);
    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值