lightoj 1349 Aladdin and the Optimal Invitation

Aladdin and the Optimal Invitation

Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

Finally Aladdin reached home, with the great magical lamp. He was happier than ever. As he was a nice boy, he wanted to share the happiness with all people in the town. So, he wanted to invite all people in town in some place such that they can meet there easily. As Aladdin became really wealthy, so, number of people was not an issue. Here you are given a similar problem.

Assume that the town can be modeled as an m x n 2D grid. People live in the cells. Aladdin wants to select a cell such that all people can gather here with optimal overall cost. Here, cost for a person is the distance he has to travel to reach the selected cell. If a person lives in cell (x, y) and he wants to go to cell (p, q), then the cost is |x-p|+|y-q|. So, distance between (5, 2) and (1, 3) is |5-1|+|2-3| which is 5. And the overall cost is the summation of costs for all people.

So, you are given the information of the town and the people, your task to report a cell which should be selected by Aladdin as the gathering point and the overall cost should be as low as possible.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a blank line. Next line contains three integers: m,n and q (1 ≤ m, n, q ≤ 50000)m and ndenote the number of rows and columns of the grid respectively. Each of the next q lines contains three integers u v w (1 ≤ u ≤ m, 1 ≤ v ≤ n, 1 ≤ w ≤ 10000), meaning that there are w persons who live in cell (u, v). You can assume that there are no people in the cells which are not listed. You can also assume that each of the q lines contains a distinct cell.

Output

For each case, print the case number and the row and column position of the cell where the people should be invited. There can be multiple solutions, any valid one will do.

Sample Input

2

 

5 1 1

2 1 10

 

5 5 4

1 1 1

2 2 1

4 4 1

5 5 1

Sample Output

Case 1: 2 1

Case 2: 3 3




有一个简单的数论定理 :  |x-x1|+|x-x2|+|x-x3|+|x-x4|+|x-x5|+|x-x6|+...的最小值是x=(x1,x2,x3,x4,x5,...)的中位数的时候  

然后这题就可以做了。


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=50005;


struct node
{
    int x,y,num;
}a[maxn];
int n,m,q;


bool cmp1(node p,node q)
{
    return p.x<q.x;
}


bool cmp2(node p,node q)
{
    return p.y<q.y;
}


int main()
{
    int T;
    scanf("%d",&T);
    for(int co=1;co<=T;co++)
    {
         int num=0,ans_x,ans_y,t=0;
         scanf("%d %d %d",&n,&m,&q);
         for(int i=0;i<q;i++)
         {
              scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].num);
              num+=a[i].num;
         }
         num=(num+1)/2;
         sort(a,a+q,cmp1);
         for(int i=0;i<q;i++)
         {
               t+=a[i].num;
               if(t>=num)
               {
                    ans_x=a[i].x;
                    break;
               }
         }
         sort(a,a+q,cmp2);
         t=0;
         for(int i=0;i<q;i++)
         {
               t+=a[i].num;
               if(t>=num)
               {
                    ans_y=a[i].y;
                    break;
               }
         }
         printf("Case %d: %d %d\n",co,ans_x,ans_y);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值