G - Aladdin and the Optimal Invitation
Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1349 uDebug
Appoint description: 
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 n denote 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 theq 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

Hint

1.      This is a special judge problem; wrong output format may cause 'Wrong Answer'.

2.      Dataset is huge, use faster I/O methods.

Problem Setter: Jane Alam Jan
Developed and Maintained by 
JANE ALAM JAN
题意:
给定一个n*m的图,图上有q个点住有人。现在已经给出每个点的坐标和该点的人数,让你找到一个点使得图中所有人到达该点的距离之和最小。

分析:行列的中位数坐标就行

java超内存代码,改为C++便可以

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {


static final int mod=1000003;

static final int N = 50005;
static int []numx=new int[N];
static int []numy=new int[N];

public static void main(String args[])throws IOException {

Scanner in = new Scanner(System.in);
int t=in.nextInt();
int kase=0;
while(t-->0)
{
int n=in.nextInt();
int m=in.nextInt();
int q=in.nextInt();
int sum=0;

for(int i=1;i<=n;i++)
numx[i]=0;
for(int i=1;i<=m;i++)
numy[i]=0;
while(q-->0)
{
int u=in.nextInt();
int v=in.nextInt();
int w=in.nextInt();
numx[u]+=w;
numy[v]+=w;
sum+=w;
}
int mid=(sum+1)>>1;
int num=0;
int x=0,y=0;
for(int i=1;i<=n;i++)
{
num+=numx[i];
if(num>=mid)
{
x=i;
break;
}
}
num=0;
for(int i=1;i<=m;i++)
{
num+=numy[i];
if(num>=mid)
{
y=i;
break;
}
}
kase++;
System.out.println("Case "+kase+": "+x+" "+y);
}

}



}
#include <cstdio>
#include <cstring>
#define N 50010
int rnum[N], cnum[N];
int main()
{
int t, Q=1; scanf("%d", &t);
while(t--)
{
int m, n, q;
scanf("%d%d%d", &m, &n, &q);
memset(rnum, 0, sizeof(rnum));
memset(cnum, 0, sizeof(cnum));
int sum=0;
while(q--)
{
int r, c, p;
scanf("%d%d%d", &r, &c, &p);
sum=sum+p;
rnum[r]+=p;
cnum[c]+=p;
}
int ansr, ansc;
int num=0;
int mid=(sum+1)/2; /***奇数--> a-b=1 ;*/
for(int i=1; i<= m; i++)
{
num+=rnum[i];
if(num>=mid)
{
ansr=i;
break;
}
}
num=0;
for(int i=1; i<=n; i++)
{
num+= cnum[i];
if(num>=mid)
{
ansc=i;
break;
}
}
printf("Case %d: %d %d\n", Q++, ansr, ansc);
}
return 0;
}