zoukankan      html  css  js  c++  java
  • hdu-5681 zxa and wifi(dp)

    题目链接:

    zxa and wifi

    Time Limit: 2000/1000 MS (Java/Others)    

    Memory Limit: 65536/65536 K (Java/Others)


    Problem Description
     
    zxa went to Q town as a volunteer, and the town mayor intended to achieve network coverage for the n families living in the town. This n families are able to be seen as the points in the axis, and the families from east to west are numbered from 1 to n, where the distance between the i-th family and the (i+1)-th family is di(1i<n).

    zxa was in charge of the planning of this project, and he was informed that the carriers were given two ways to set up the network. One way is using one wireless router and cables associated at the i-th family for some families network coverage, where the distance from the i-th family to each covered family (include the i-th family) is no more than ri , which needs ai costs. Another way is using one optical fiber cable at the i-th family for the i-th family network coverage, which needs bi costs.

    zxa is interested to know, assuming that it is only permitted to use at most k wireless routers for network coverage in order to avoid too large Wi-Fi radiation, then what is the minimum cost for this n families network coverage, can you help him?
     
    Input
     
    The first line contains an positive integer T, represents there are T test cases.

    For each test case:

    The first line contains two positive integers n and k.

    The second line contains (n1) positive integers, represent d1,d2,,dn1.

    The next n lines, the i-th line contains three positive integers ai,ri and bi.

    There is a blank between each integer with no other extra space in one line.

    1T100,2n210^4,1kmin(n,100),1ai,bi,di,ri10^5,1n10^5
     
    Output
     
    For each test case, output in one line a positive integer, repersents the minimum cost for this n families network coverage.
     
    Sample Input
     
    2
    2 1
    1
    12 11 3
    1 7 4
    5 5
    7 4 8 6
    13 6 3
    14 2 3
    3 6 4
    11 12 2
    9 14 4
     
    Sample Output
     
    1
    12
     
     
     
    题意:
     
    第i户与第i+1户相距d[i],第i户装光缆需钱b[i],装WiFi需要a[i],且r[i]范围内的用户都可以用,WiFi的个数不超过k,问使全都能上网的最小花费;
     
    思路:
     
    dp[i][j]表示装i个WiFi使的前j户可以上网的最小花费;
    对于第j户可以有两种选择,装光缆dp[i][j]=min(dp[i][j],dp[i][j-1]+b[j])
    装WiFi  dp[i][r[j]]=min(dp[i][r[j]],dp[i][x]+a[j])  l[i]-1<=x<=j;
    还有就是先处理出第i户装WiFi时它能作用的范围[l[i],r[i]];
    ans=min(ans,dp[i][n])0<=i<=k;
     
     
    AC代码:
     
    //#include <bits/stdc++.h>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
     int inf=0x3f3f3f3f;
    const int N=2e4+25;
    int n,k,a[N],b[N],sum[N],dis[N],l[N],r[N],d[N];
    int dp[102][N];
    int main()
    {
        inf*=2;
       int t;
       scanf("%d",&t);
       while(t--)
       {
           mst(dp,inf);
           scanf("%d%d",&n,&k);
           sum[1]=sum[0]=0;
           Riep(n-1)scanf("%d",&d[i]),sum[i+1]=sum[i]+d[i];
           Riep(n)scanf("%d%d%d",&a[i],&dis[i],&b[i]);
           Riep(n)
           {
               int L=1,R=i;
               while(L<=R)
               {
                   int mid=(L+R)>>1;
                   if(sum[i]-sum[mid]>dis[i])L=mid+1;
                   else R=mid-1;
               }
               l[i]=L;
               L=i,R=n;
               while(L<=R)
               {
                   int mid=(L+R)>>1;
                   if(sum[mid]-sum[i]>dis[i])R=mid-1;
                   else L=mid+1;
               }
               r[i]=R;
           }
           for(int i=0;i<=k;i++)dp[i][0]=0;
           for(int i=1;i<=n;i++)dp[0][i]=min(dp[0][i],dp[0][i-1]+b[i]);
           for(int i=1;i<=k;i++)
           {
               Rjep(n)
               {
                   dp[i][j]=min(dp[i][j],dp[i][j-1]+b[j]);
                   for(int x=l[j]-1;x<=j;x++)
                   {
                       dp[i][r[j]]=min(dp[i][r[j]],dp[i-1][x]+a[j]);
                   }
               }
           }
           int ans=inf;
           for(int i=0;i<=k;i++)
           {
               ans=min(ans,dp[i][n]);
           }
           printf("%d
    ",ans);
       }
        return 0;
    }
  • 相关阅读:
    Java入门系列-10-数组
    Java入门系列-09-循环结构
    Java入门系列-08-选择结构
    Java入门系列-07-从控制台中接收输入
    Java入门系列-06-运算符
    05-数据类型和类型转换
    004 java中的变量
    Filter过滤器实现同一地址手机和电脑页面不同
    Java 8 Lambda表达式10个示例【存】
    Centos7+Apache2.4+php5.6+mysql5.5搭建Lamp环境——为了wordPress
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5494853.html
Copyright © 2011-2022 走看看