POJ 2947 Widget Factory(取模的高斯消元)

畫船聽雨發表於2014-07-24
題目大意:有n個裝飾品,每個裝飾品要生產3~9天。給出m種作業,每個作業生產k種裝飾品,從星期X生產到星期Y(未必是同一個星期,一天只能生產一個產品),然後給出這k種裝飾品分別是什麼。問是否能求出n個裝飾品分別須要多少天來生產,若有多組解輸出Multiple solutions.,無解輸出Inconsistent data.。
思路:可以列出m個方程組成方程組。對於每一個作業,設ki為生產裝飾品 i 多少次(用輸入資料統計一下就好),xi為生產裝飾品 i 需要多少天,那麼可以列出方程

k1 * x1 + k2 * x2 + …… + kn * xn = Y - X + 1(mod 7)。

Widget Factory
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 4417   Accepted: 1493

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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x7fffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

const int maxn = 340;

using namespace std;

int a[maxn][maxn];
int x[maxn];
int equ, var;
char str1[maxn], str2[maxn];

int LCM(int a, int b)
{
    return (a/(__gcd(a, b)))*b;
}

int Gauss()
{
    int row, col, max_r;
    row = col = 0;
    while(row < equ && col < var)
    {
        max_r = row;
        for(int i = row+1; i < equ; i++)
            if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i;
        if(max_r != row)
            for(int j = col; j <= var; j++) swap(a[row][j], a[max_r][j]);
        if(a[row][col] == 0)
        {
            col++;
            continue;
        }
        for(int i = row+1; i < equ; i++)
        {
            if(a[i][col] == 0) continue;
            int l = LCM(abs(a[row][col]), abs(a[i][col]));
            int ta = l/a[i][col];
            int tb = l/a[row][col];
            if(ta*tb < 0) tb *= -1;///判斷是否異號
            for(int j = col; j <= var; j++)
                a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%7 + 7)%7;
        }
        row++;
        col++;
    }
    for(int i = row; i < equ; i++)
        if(a[i][col] != 0) return -1;
    if(row < var)
        return var-row;
    for(int i = var-1; i >= 0; i--)///根據上三角陣,迭代求出每一次的值
    {
        int tmp = a[i][var];
        for(int j = i+1; j < var; j++)
            if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%7 + 7)%7;
        while(tmp%a[i][i] != 0)
            tmp += 7;
        x[i] = tmp/a[i][i]%7;
    }
    return 0;
}

void init()
{
    memset(x, 0, sizeof(x));
    memset(a, 0, sizeof(a));
}

int Tr(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;
    if(strcmp(s,"SUN") == 0)
        return 7;
    return 0;
}

int main()
{
    int n, m;
    while(~scanf("%d %d",&n, &m))
    {
        if(!n && !m)
            break;
        init();
        int t;
        int tmp;
        for(int i = 0; i < m; i++)
        {
            cin >>t>>str1>>str2;
            a[i][n] = ((Tr(str2)-Tr(str1)+1)%7+7)%7;
            while(t--)
            {
                scanf("%d",&tmp);
                tmp--;
                a[i][tmp]++;
                a[i][tmp] %= 7;
            }
        }
        equ = m;
        var = n;
        int flag = Gauss();
        if(flag == -1)
            printf("Inconsistent data.\n");
        else if(flag > 0)
            printf("Multiple solutions.\n");
        else
        {
            for(int i = 0; i < var; i++)
                if(x[i] <= 2)
                    x[i] += 7;///根據題意要求天數必須在3----9之間
            for(int i = 0; i < var-1; i++)
                printf("%d ",x[i]);
            printf("%d\n",x[var-1]);
        }
    }
    return 0;
}


相關文章