ZOJ 1409 Communication System
This is a greedy algorithm, but some processing is required before greed.
First, the question requires B/P to be as big as possible, so P should be as small as possible, and B should be as big as possible. However, the processing method of B and P is different. B is the smallest of all bandwidths, and P is the total price of all devices. So I can think of a way to enumerate B one by one, I did not dare to think like this, but the question was given for a long time and I thought it should not be a problem. When I ran it, it was only 0 ms, which surprised me. Then select the device, and then use greedy:
Given a band, for a device, it is obvious that the bandwidth is larger than the band in the selection of different manufacturers, in which the price is the cheapest.
My specific handling details are as follows:
1. Sort all the bands in ascending order. When enumeration starts from the smallest one, when enumeration reaches a band, a device cannot be selected, that is to say, the bandwidth of each manufacturer of the device is not large, so it is over.
2. Sort the bands of each device in descending order, which makes it easier to find the smallest price.
#include
#include
#include
#include#include
#include
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
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--) {www.bkjia.com 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