poj 2947 Widget Factory(高斯消元解同余方程组)

17 篇文章 0 订阅
Widget Factory
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 4260 Accepted: 1422

Description

The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to build a widget depends on its type: the simple widgets need only 3 days, but the most complex ones may need as many as 9 days.

The factory is currently in a state of complete chaos: recently, the factory has been bought by a new owner, and the new director has fired almost everyone. The new staff know almost nothing about building widgets, and it seems that no one remembers how many days are required to build each diofferent type of widget. This is very embarrassing when a client orders widgets and the factory cannot tell the client how many days are needed to produce the required goods. Fortunately, there are records that say for each widgeteer the date when he started working at the factory, the date when he was fired and what types of widgets he built. The problem is that the record does not say the exact date of starting and leaving the job, only the day of the week. Nevertheless, even this information might be helpful in certain cases: for example, if a widgeteer started working on a Tuesday, built a Type 41 widget, and was fired on a Friday,then we know that it takes 4 days to build a Type 41 widget. Your task is to figure out from these records (if possible) the number of days that are required to build the different types of widgets.

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 300 of the different types, and the number 1 ≤ m ≤ 300 of the records. This line is followed by a description of the m records. Each record is described by two lines. The first line contains the total number 1 ≤ k ≤ 10000 of widgets built by this widgeteer, followed by the day of week when he/she started working and the day of the week he/she was fired. The days of the week are given bythe strings `MON', `TUE', `WED', `THU', `FRI', `SAT' and `SUN'. The second line contains k integers separated by spaces. These numbers are between 1 and n , and they describe the diofferent types of widgets that the widgeteer built. For example, the following two lines mean that the widgeteer started working on a Wednesday, built a Type 13 widget, a Type 18 widget, a Type 1 widget, again a Type 13 widget,and was fired on a Sunday.

4 WED SUN
13 18 1 13

Note that the widgeteers work 7 days a week, and they were working on every day between their first and last day at the factory (if you like weekends and holidays, then do not become a widgeteer!).

The input is terminated by a test case with n = m = 0 .

Output

For each test case, you have to output a single line containing n integers separated by spaces: the number of days required to build the different types of widgets. There should be no space before the first number or after the last number, and there should be exactly one space between two numbers. If there is more than one possible solution for the problem, then write `Multiple solutions.' (without the quotes). If you are sure that there is no solution consistent with the input, then write `Inconsistent data.'(without the quotes).

Sample Input

2 3
2 MON THU
1 2
3 MON FRI
1 1 2
3 MON SUN
1 2 2
10 2
1 MON TUE 
3
1 MON WED
3
0 0

Sample Output

8 3
Inconsistent data.

Hint

Huge input file, 'scanf' recommended to avoid TLE.  


题意:有n种装饰品,工人制造装饰品最小花费3天,最多花费9天。现给出m条记录,分别表示m个工人的工作记录,包括完成了多少个装饰品、什么时候开始工作、什么时候被辞退和完成了哪些种类的装饰品。要根据这些记录求出每种装饰品各需花费多少天来完成。
思路:高斯消元解同余方程组。根据每一条记录列出一条方程,例如有一条记录:完成了3个装饰品,星期一开始工作,星期四被辞退,完成的是第1、1、2种装饰品,那么可列出一条方程:(2x1+x2)%7=4,以此类推。

AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const int INF = 2000000000;
const int maxn = 350;

int a[maxn][maxn];
int n, m, k;
int getdate(char *s){
    if(strcmp(s, "MON") == 0) return 1;
    if(strcmp(s, "TUE") == 0) return 2;
    if(strcmp(s, "WED") == 0) return 3;
    if(strcmp(s, "THU") == 0) return 4;
    if(strcmp(s, "FRI") == 0) return 5;
    if(strcmp(s, "SAT") == 0) return 6;
    return 7;
}
void gauss(){
    int ans[maxn];
    int row = 0, col = 0;
    while(row < m && col < n)
    {
        int id = row;
        for(int i = row + 1; i < m; i++)
        if(abs(a[i][col]) > abs(a[id][col]))
        id = i;
        if(a[id][col])
        {
            for(int i = col; i <= n; i++)
            swap(a[id][i], a[row][i]);
            for(int i = row + 1; i < m; i++)
            if(a[i][col])
            {
                int b1 = a[row][col], b2 = a[i][col];
                for(int j = col; j <= n; j++)
                a[i][j] = ((a[i][j] * b1 - a[row][j] * b2) % 7 + 7) % 7;
            }
            row++;
        }
        col++;
    }
    for(int i = row; i < m; i++)            
    if(a[i][n])                         //无解
    {
        printf("Inconsistent data.\n");
        return;
    }
    if(row < n)                         //多种解
    {
        printf("Multiple solutions.\n");
        return;
    }
    for(int i = n - 1; i >= 0; i--)       //回代
    {
        int tmp = a[i][n];
        for(int j = i + 1; j < n; j++)
        tmp = ((tmp - a[i][j] * ans[j]) % 7 + 7) % 7;
        while(tmp % a[i][i] != 0) tmp += 7;
        ans[i] = (tmp / a[i][i]) % 7;
    }
    for(int i = 0; i < n; i++)
    if(ans[i] < 3) ans[i] += 7;
    for(int i = 0; i < n - 1; i++)
    printf("%d ",ans[i]);
    printf("%d\n", ans[n - 1]);
}
int main()
{
    char s1[5], s2[5];
    int type;
    while(scanf("%d%d", &n, &m), n || m)
    {
        memset(a, 0, sizeof(a));
        for(int i = 0; i < m; i++)
        {
            scanf("%d%s%s", &k, s1, s2);
            a[i][n] = (getdate(s2) - getdate(s1) + 7 + 1) % 7;
            while(k--)
            {
                scanf("%d", &type);
                a[i][type - 1]++;
            }
            for(int j = 0; j < n; j++) a[i][j] %= 7;
        }
        gauss();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值