Sicily 1025. Parallel Challenge B

来源:互联网 发布:苏州二手房成交数据 编辑:程序博客网 时间:2024/06/11 07:43

1025. Parallel Challenge B

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The 2005 ACM-ICPC World Finals was held in Shanghai in April. It was a wonderful trip for Pegasus and among all the interesting activities, the IBM Parallel Challenge ballgame was the most impressive.

 The Parallel Challenge ball game gives each team a chance to pit your programming skills against those of other teams in a fast-moving parallel-programming game of skill, treachery, and hazard avoidance. Each team will write a single C++ class named MyPlayer which defines the characteristics of a “player”. The player code will be instantiated five times in the environment, making up a five-play team which will then compete in a series of Parallel Challenge ballgames running on an IBM Power Architecture Blue Gene supercomputer-the world’s fastest computer. Each of the players will run in parallel on its own separate dedicated processor in the Blue Gene.

 Each Parallel Challenge ballgame consists of a number of different teams attempting to score points while at the same time trying to hinder other teams’ ability to do so, and all the while trying also to avoid numerous hazards on the Parallel Challenge ballgame field. In each game, carrying or throwing a ball into a goal is an example of one way points can be scored.

 The objective is to write your MyPlayer class so that the players operate in a coordinated fashion, taking advantage of the various ways to score points while at the same time avoiding both hazards on the game field and impediments thrown at them by players form other teams(and, in some cases, by spectators!). The winner of a game is the team whose players score the largest total number of points.

 After the coding phase, each team must submit a final version of the MyPlayer class to the Blue Gene. The final submitted version from each team will then be run in a tournament which will take place at the Parallel Challenge Dinner in the evening. The tournament will consist of a series of rounds. Each round will consist of several Parallel Challenge ballgames between randomly chosen sets of teams. Each team’s players will participate in a least three games.

 At the end of each round teams will be ranked according to the total number of points accumulated during the games played in the round, and the top teams will advance to the next round. Each team advancing to a new round will start the new round with zero points and will be randomly regrouped with a new set of opponents. The team whose players earn the most points in the final tournament round will be the 2005 Parallel Challenge Champions.

 Now, here comes the problem:

 In the coding phase, Pegasus takes a very simple strategy to get the ball: Once the ball appears, all the players on the football field run to the position where the ball is. To simplify this problem, all the players can just run horizontally or vertically. Fig 1 shows the feasible routes of the four players. However, players lose w calories of energy while running per meter(w may be different for different players). Given the positions of all players, where should Pegasus put the ball in order to minimize the total energy loss? Assume that you can put the ball on arbitrary position as you like.

 Pegasus is stumped by this problem now. So they hope you, a distinguished programmer, to help the three poor guys to solve it.

Input

There are several test cases in the input file. The first line of each case is an integerN (1<=N<=10000), indicating the number of players on the field. N integer numbers follow, w_i, ranging from 1 to 1000 – The energy that the ith player loses while running per meter. Then there are N pairs of integers, x_i, y_i (measured in meter and 0<=x_i, y_i<=100000), indicating the coordinate of each player.
Process to the end of the file.

Output

Output the minimum total energy loss in a single line, with 2 digits after the decimal point.
Note: the minimum total energy loss means the minimum value of  by choosing the proper position (x, y) of the ball.

Sample Input

1510 1021 20 00 1031 2 30 01 105 5

Sample Output

0.0010.0028.00

Hint

In the third example, we can put the ball in the position (1, 5).

Problem Source

ZSUACM Team Member

// Problem#: 1025// Submission#: 3223309// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>#include <cmath>using namespace std;#define MAX 10005struct posAndVal {    int p;    int v;};int N;posAndVal x[MAX];posAndVal y[MAX];int val[MAX];long long valFrontSum[MAX];long long valBackSum[MAX];long long xvalFrontSum[MAX];long long xvalBackSum[MAX];long long yvalFrontSum[MAX];long long yvalBackSum[MAX];bool cmp(const posAndVal & a, const posAndVal & b) {    return a.p < b.p;}void initX() {    int i;    for (i = 1, valFrontSum[0] = 0; i < N; i++) {        valFrontSum[i] = valFrontSum[i - 1] + x[i - 1].v;    }    for (i = N - 2, valBackSum[N - 1] = 0; i >= 0; i--) {        valBackSum[i] = valBackSum[i + 1] + x[i + 1].v;    }    for (i = 1, xvalFrontSum[0] = 0; i < N; i++) {        xvalFrontSum[i] = xvalFrontSum[i - 1] + valFrontSum[i] * (x[i].p - x[i - 1].p);    }    for (i = N - 2, xvalBackSum[N - 1] = 0; i >= 0; i--) {        xvalBackSum[i] = xvalBackSum[i + 1] + valBackSum[i] * (x[i + 1].p - x[i].p);    }}void initY() {    int i;    for (i = 1, valFrontSum[0] = 0; i < N; i++) {        valFrontSum[i] = valFrontSum[i - 1] + y[i - 1].v;    }    for (i = N - 2, valBackSum[N - 1] = 0; i >= 0; i--) {        valBackSum[i] = valBackSum[i + 1] + y[i + 1].v;    }    for (i = 1, yvalFrontSum[0] = 0; i < N; i++) {        yvalFrontSum[i] = yvalFrontSum[i - 1] + valFrontSum[i] * (y[i].p - y[i - 1].p);    }    for (i = N - 2, yvalBackSum[N - 1] = 0; i >= 0; i--) {        yvalBackSum[i] = yvalBackSum[i + 1] + valBackSum[i] * (y[i + 1].p - y[i].p);    }}long long findMin() {    initX();    long long minAns = -1;    long long ans;    for (int i = 0; i < N; i++) {        long long temp = xvalFrontSum[i] + xvalBackSum[i];        if (minAns == -1 || minAns > temp) minAns = temp;    }    ans = minAns;    minAns = -1;    initY();    for (int i = 0; i < N; i++) {        long long temp = yvalFrontSum[i] + yvalBackSum[i];        if (minAns == -1 || temp < minAns) minAns = temp;    }    ans += minAns;    return ans;}int main() {    //std::ios::sync_with_stdio(false);    while (scanf("%d", &N) != EOF) {        for (int i = 0; i < N; i++) cin >> val[i];        for (int i = 0; i < N; i++) {            scanf("%d%d", &x[i].p, &y[i].p);            x[i].v = y[i].v = val[i];        }        if (N == 1) {            printf("0.00\n");            continue;        }        sort(x, x + N, cmp);        sort(y, y + N, cmp);        printf("%lld.00\n", findMin());    }    //getchar();    //getchar();        return 0;}                                 


0 0