ZOJ 1409 Communication System

Source: Internet
Author: User

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
       
        

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.