uestc 1650 Electric System Restore(中位数定理?)

Electric System Restore

Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 32 Tried: 311

Submit

Status

Best Solution

Back

Description

After building and improving the traffic system of Country X, Qiqi wants to restore the electric system. Every City in Country X can be supplied by the base station or itself. The cost of transportation between base station and a city is the distance between them, i.e. if their coordinates are (x1, y1) and (x2, y2), their distance equals to |x1 - x2| + |y1 - y2|. Also, it costs Ci to power itself for the ith city. 

Due to some reasons, the number of cities powering themselves should be no more than K. As the chief designer of Country X, it's your job to place the base station and assign the cities powering themselves to minimize the total cost. Note that you can place the base station at any integer point.

Input

There are multiple test cases. The first line of the input will be an integer T (T <= 20) indicating the number of test cases. 

For each test case there are two integers N and K (1 <= N <= 1000, 0 <= K <= 20, K <= N) in a single line, representing the number of cities in Country X and Qiqi allows at most K cities power itself. The next line lists N integers, Ci, describing the cost of power itself of each city. Then N lines follow, each contains two integers Xi and Yi, indicating the coordinate of the ith city. 0 <= Xi, Yi, Ci <= 106 for 1 <= i <= N.

Output

For each test case, print "Case #t: " first, in which t is the number of the test case starting from 1. Then output the minimum costs.

Sample Input

1
4 1
2 2 2 2
1 1
1 2
2 1
2 3

Sample Output

Case #1: 4

Hint

For the first sample, we should build the base station at (1,1) and make the 4th city power itself.

Source

10th UESTC Programming Contest Final

题目:http://acm.uestc.edu.cn/problem.php?pid=1650

题意:给你n个城市的坐标,你可以随便选一个位置建立电站,来给n个城市供电,有的城市也可以自己供电,但最多只能有k个城市自己供电

分析:当k等于0时,很显然,这个电站必须建在所有点x轴的中位数,和y轴的中位数,但是如何考虑k个城市自己供电,使得答案更优呢

今天的模拟赛时,没细心去想,其实删掉k个点给中位数带来的波动不大,就原来中位数的左右k/2+1的波动,由于k相当小,直接暴力枚举所有电站的位置,然后统计答案即可,怎么统计不用我说了吧。。。。

PS:通过今天的模拟比赛,发现我还是弱得不行啊

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int mm=1111;
int x[mm],y[mm],a[mm],b[mm],c[mm];
int i,j,n,m,t,l,r,ans,cs=0;
void work(int x0,int y0)
{
    priority_queue<int>q;
    int i,sum;
    for(sum=i=0;i<n;++i)
    {
        sum+=fabs(x[i]-x0)+fabs(y[i]-y0);
        q.push(c[i]-fabs(x[i]-x0)-fabs(y[i]-y0));
        if(q.size()>m)q.pop();
        if(!q.empty()&&q.top()>=0)q.pop();
    }
    while(!q.empty())sum+=q.top(),q.pop();
    ans=min(ans,sum);
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;++i)
            scanf("%d",&c[i]);
        for(i=0;i<n;++i)
            scanf("%d%d",&x[i],&y[i]),a[i]=x[i],b[i]=y[i];
        sort(a,a+n);
        sort(b,b+n);
        l=max(0,n/2-m/2-1);
        r=min(n-1,n/2+m/2+1);
        ans=2e9;
        for(i=l;i<=r;++i)
            for(j=l;j<=r;++j)
                work(a[i],b[j]);
        printf("Case #%d: %d\n",++cs,ans);
    }
    return 0;
}


你可能感兴趣的:(Integer,equals,System,input,each,distance)