Sicily 1739. Helpful papers

1739. Helpful papers

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

How time flies! Sunshine, an intelligent girl, becomes a senior student now. Recently, she is preparing for her thesis. She made a plan to read many papers, because only in this way can she master the research trends in her interesting field and form her own ideas. 
Now Sunshine has downloaded many papers. Every paper has only one author, and maybe some of them were written by the same person. Sunshine wants to read each author’s paper according to the time these papers were written. 
For example, Sunshine has five papers: P1 (written by A in 1981), P2 (written by B in 1982), P3 (written by A in 1986), P4 (written by B in 1989), P5 (written by A in 1999).
Sunshine should read P1 before reading P3, P3 before reading P5, and P2 before reading P4. So all the possibility of the schedules are list here:
P1->P3->P5->P2->P4          P1->P3->P2->P5->P4
P1->P2->P3->P5->P4          P2->P1->P3->P5->P4
P1->P3->P2->P4->P5          P1->P2->P3->P4->P5
P2->P1->P3->P4->P5          P1->P2->P4->P3->P5
P2->P1->P4->P3->P5          P2->P4->P1->P3->P5
What’s more, each paper has two parameters (Wi, Ti). Ti is the time of reading the paper and Wi is an added value that is used to represent the importance of the paper. Now Sunshine asks you to give her a schedule S that Cost(S) is minimal. 
For schedule S(P1, P2, …, Pn), 
Cost(S)=W1*T1+W2*(T1+T2)+W3*(T1+T2+T3)+…+Wn*(T1+T2+…+Tn). 

Input

The first line of the input is a positive integer C which is the number of the test cases followed. Each test case contains two parts. 
The first line of the first part is a positive integer N (0<N<=100) which represents the number of the papers. After that, N lines are followed. The ith line contains two positive integers Wi (0<Wi<=100), Ti(0<Ti<=100) for the ith paper. 
The first line of the second part is a positive integer M (0<M<=100) which means the number of the researchers. And the following M lines inform you the papers that were written by the researchers. The first number of the ith line is an positive number Ai (0<Ai<=100) telling you the number of the papers the ith researchers wrote. After that there are Ai numbers that represent the IDs of the papers according to the time they were written. 

Output

The output of the program should consist of one line of output for each test case. The output of each test case only contains an integer which represents the minimal Cost(S). No redundant spaces are needed.

Sample Input

1
7
6 3
18 6
12 6
8 5
8 4
17 8
18 10
2
4 1 2 3 4
3 5 6 7

Sample Output

1967

// Problem#: 1739
// Submission#: 3584618
// 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 <stdio.h>
#include <string.h>

unsigned short n, m, w[101], t[101];
unsigned short a[101], id[101][101];

void input() {
    int i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++) scanf("%d%d", &w[i], &t[i]);
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
        scanf("%d", &a[i]);
        for (j = 0; j < a[i]; j++) {
            scanf("%d", &id[i][j]);
            id[i][j]--;
        }
    }
}

int cal() {
    unsigned short ans[1010];
    unsigned short p[101], maxp[101], maxv[101][2];
    int i, j, t0, t1, temp, choice, ret, tot;
    
    tot = 0;
    memset(p, 0, sizeof(p));
    for (i = 0; i < m; i++) {
        maxp[i] = p[i];
        t0 = maxv[i][0] = w[id[i][p[i]]];
        t1 = maxv[i][1] = t[id[i][p[i]]];
        for (j = p[i] + 1; j < a[i]; j++) {
            t0 += w[id[i][j]];
            t1 += t[id[i][j]];
            if (t0 * maxv[i][1] > t1 * maxv[i][0]) {
                maxp[i] = j;
                maxv[i][0] = t0;
                maxv[i][1] = t1;
            }
        }
    }
    while (1) {
        choice = -1;
        for (i = 0; i < m; i++)
            if (p[i] < a[i] && (choice == -1 || maxv[choice][0] * maxv[i][1] < maxv[choice][1] * maxv[i][0])) {
                choice = i;
            }
        if (choice == -1) break;
        for (i = p[choice]; i <= maxp[choice]; i++) ans[tot++] = id[choice][i];
        
        p[choice] = maxp[choice] + 1;
        maxp[choice] = p[choice];
        t0 = maxv[choice][0] = w[id[choice][p[choice]]];
        t1 = maxv[choice][1] = t[id[choice][p[choice]]];
        for (i = p[choice] + 1; i < a[choice]; i++) {
            t0 += w[id[choice][i]];
            t1 += t[id[choice][i]];
            if (t0 * maxv[choice][1] > t1 * maxv[choice][0]) {
                maxp[choice] = i;
                maxv[choice][0] = t0;
                maxv[choice][1] = t1;
            }
        }
    }
    ret = temp = 0;
    for (i = 0; i < tot; i++) {
        temp += t[ans[i]];
        ret += w[ans[i]] * temp;
    }
    return ret;
}

int main() {
    int cases;
    scanf("%d", &cases);
    while (cases--) {
        input();
        printf("%d\n", cal());
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值