当前位置: 首页 > 工具软件 > Aladdin lamp > 使用案例 >

N - Aladdin and the Optimal Invitation LightOJ - 1349

子车睿
2023-12-01

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 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

Hint

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

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

 题意:有一个人他很高兴想要邀请镇上的人在一个地方聚会,题目先输入m,n,q;分别代表了矩阵的大小和镇上的人数

然后输入q个人的所在的坐标,每个人到达目的地的耗力为横纵坐标差值的绝对值,问是否存在一个地方是所有人到这的耗力最小;

思路:试了试中点,就是对x排一下序,取其中点,同理y,这个坐标就是所要求的答案;

下面附上代码:

#include<bits/stdc++.h>
using namespace std;
const int N=50000+50;
struct node
{
	int u,v,w;
}s[N];
bool cmp1(node A,node B)
{
	return A.u<B.u;
}
bool cmp2(node A,node B)
{
	return A.v<B.v;
}
int main()
{
	int n,m,q,t,k=0;
	cin>>t;
	while(t--)
	{
		int l,h;
		cin>>n>>m>>q;
		int sum=0,sum1=0,sum2=0;
		for(int i=0;i<q;i++)
		{
			cin>>s[i].u>>s[i].v>>s[i].w;
			sum+=s[i].w;	
		}	
		sum/=2;
		sort(s,s+q,cmp1);
		for(int i=0;i<q;i++)
		{
			sum1+=s[i].w;
			if(sum1>sum)
			{
				l=s[i].u;
				break;
			}
		}
		sort(s,s+q,cmp2);
		for(int i=0;i<q;i++)
		{
			sum2+=s[i].w;
			if(sum2>sum)
			{
				h=s[i].v;
				break;
			}
		}
		printf("Case %d: %d %d\n",++k,l,h);		
	}	
	return 0;
}









 类似资料: