您的位置:首页 > 编程语言

PKU ACM 题目1018 源代码

2010-12-12 13:15 225 查看
该问题主要涉及排序效率问题,开始使用在读入数据过程中创建最小堆,但是会导致超时,后改成读入数据后调用qsort函数进行排序,时间减小到了79ms。

因为可能的带宽值有很多重复值会导致重复计算,因此在贪心计算前先保证不会重复,这样提升到了16ms。

源代码如下:

Problem

Result
Memory
Time
Language
Code Length
1018
Accepted
476K
16MS
GCC
6100B
1018
Accepted
476K
79MS
GCC
5926B
1018
Time Limit Exceeded
GCC
9754B
/*Communication System
Time Limit: 1000MS  Memory Limit: 10000K
Total Submissions: 13870  Accepted: 4836

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
*/

#include <stdio.h>
#include "stdlib.h"

#define MAX_NUMBER_OF_DEVICES 100

#define MAX_NUMBER_OF_MANUFACTURERS  100

#define MAX_NUMBER_OF_BANDWIDTH 10000

typedef struct  _MANUFACTURER_ST_
{
int iBandwidth;
int iPrice ;
//int iNextMaxBandwidthIndex;
}MANUFACTURER_ST;
typedef struct _COMMUNICATION_SYSTEM_INFO_ST_
{
int iDeviceNum;

int iMinPossibleBandwidthIndex;
int iPossibleBandWidthNum;
int aiPossibleBandWidth[MAX_NUMBER_OF_BANDWIDTH];
struct
{
int iManufacturerNumber;
//int iMaxBandwidthIndex;
MANUFACTURER_ST astManufacturer[MAX_NUMBER_OF_MANUFACTURERS];
}astDevice[MAX_NUMBER_OF_DEVICES];
}COMMUNICATION_SYSTEM_INFO_ST;

int cmp1(const void *a,const void *b)
{
return ((MANUFACTURER_ST *)a)->iPrice-((MANUFACTURER_ST *)b)->iPrice;
}
int cmp2(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}

/*acm1008_main*/
int main()
{
int iLoopCase;
int iLoopDevice;
int iLoopManufact;
int iLoopWidth;
int iNumberOfTestCases;
int iMinPrice;
int iTotalPrice;
double fResult = 0;
double fResultTemp = 0;
COMMUNICATION_SYSTEM_INFO_ST  stComSysInfo;

scanf("%d",&iNumberOfTestCases);

for(iLoopCase = 0; iLoopCase < iNumberOfTestCases; iLoopCase++)
{
fResult = 0;
/************************************************************************/
/* input  width                                              */
/************************************************************************/
scanf("%d",&stComSysInfo.iDeviceNum );
stComSysInfo.iPossibleBandWidthNum = 0;
stComSysInfo.iMinPossibleBandwidthIndex = 0;
for (iLoopDevice = 0; iLoopDevice < stComSysInfo.iDeviceNum; iLoopDevice++ )
{
scanf("%d", &stComSysInfo.astDevice[iLoopDevice].iManufacturerNumber);
for (iLoopManufact = 0; iLoopManufact < stComSysInfo.astDevice[iLoopDevice].iManufacturerNumber;iLoopManufact++)
{
scanf("%d %d", &stComSysInfo.astDevice[iLoopDevice].astManufacturer[iLoopManufact].iBandwidth,
&stComSysInfo.astDevice[iLoopDevice].astManufacturer[iLoopManufact].iPrice);
/************************************************************************/
/* Calculate possible bandwidth                                         */
/************************************************************************/
stComSysInfo.aiPossibleBandWidth[stComSysInfo.iPossibleBandWidthNum]
= stComSysInfo.astDevice[iLoopDevice].astManufacturer[iLoopManufact].iBandwidth;
stComSysInfo.iPossibleBandWidthNum++;
}
/************************************************************************/
/* sort by width                                                        */
/************************************************************************/
qsort(stComSysInfo.astDevice[iLoopDevice].astManufacturer,
stComSysInfo.astDevice[iLoopDevice].iManufacturerNumber,
sizeof(MANUFACTURER_ST),cmp1);
}

qsort(stComSysInfo.aiPossibleBandWidth,
stComSysInfo.iPossibleBandWidthNum,
sizeof(int),cmp2);

/************************************************************************/
/* start                                            */
/************************************************************************/
for(iLoopWidth = 0; iLoopWidth < stComSysInfo.iPossibleBandWidthNum; iLoopWidth++)
{
if (1 <= iLoopWidth)
{
if (stComSysInfo.aiPossibleBandWidth[iLoopWidth] == stComSysInfo.aiPossibleBandWidth[iLoopWidth-1])
{
continue;
}
}
iTotalPrice = 0;
for (iLoopDevice = 0; iLoopDevice < stComSysInfo.iDeviceNum; iLoopDevice++ )
{
//iMinPrice = 0xffff;
for (iLoopManufact = 0; iLoopManufact < stComSysInfo.astDevice[iLoopDevice].iManufacturerNumber; iLoopManufact++)
{
if (stComSysInfo.astDevice[iLoopDevice].astManufacturer[iLoopManufact].iBandwidth
>= stComSysInfo.aiPossibleBandWidth[iLoopWidth])
{
iMinPrice = stComSysInfo.astDevice[iLoopDevice].astManufacturer[iLoopManufact].iPrice;
break;
}
}

if (iLoopManufact >= stComSysInfo.astDevice[iLoopDevice].iManufacturerNumber)
{
goto RESULT;
}
iTotalPrice += iMinPrice;
}
fResultTemp= (double)stComSysInfo.aiPossibleBandWidth[iLoopWidth]/(double)iTotalPrice;
fResult = (fResult > fResultTemp)?fResult:fResultTemp;
}

RESULT:	printf("%.3f/n",fResult);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: