POJ 2947 Widget Factory(高斯消元)

Widget Factory

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 7171   Accepted: 2523

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种器件,每组数据给出m个人。

对于每个人,给出他完成的器件的数量和工作开始时间是周几,结束时间是周几。

接下来给出他完成的器件编号(可重复)。

已知每个器件的完成时长总是在3天~9天,所有同编号的器件完成所需时间是相同的,现在问你,是否每个器件都存在一个合法的完成时间,满足题目中所给数据,如果存在多解输出Multiple solutions.,唯一解就输出每个器件的完成时长,否则输出Inconsistent data.

思路:

明显的高斯消元。设每个器件的完成时长是xi,则对于每一个人可以建立一个方程,但是每一个方程都是模7的。所以在计算的时候都要修改。

代码(kuangbin的板子):

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
//#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define e  2.718281828459045
#define eps 1.0e-8
using namespace std;
typedef long long int LL;
typedef pair<int,int>pa;
const int MAXL(1e5);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
const int MAXN=305;
int a[MAXN][MAXN];//增广矩阵
int x[MAXN];//解集
bool free_x[MAXN];//标记是否是不确定的变元
int gcd(int a,int b)
{
    if(b == 0)
        return a;
    else
        return gcd(b,a%b);
}
inline int lcm(int a,int b)
{
    return a/gcd(a,b)*b;//先除后乘防溢出
}
// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,
//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)
//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.
int Gauss(int equ,int var)
{
    int i,j,k;
    int max_r;// 当前这列绝对值最大的行.
    int col;//当前处理的列
    int ta,tb;
    int LCM;
    int temp;
    int free_x_num;
    int free_index;

    for(int i=0; i<=var; i++)
    {
        x[i]=0;
        free_x[i]=true;
    }

    //转换为阶梯阵.
    col=0; // 当前处理的列
    for(k = 0; k < equ && col < var; k++,col++) // 枚举当前处理的行.
    {
        // 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)
        max_r=k;
        for(i=k+1; i<equ; i++)
        {
            if(abs(a[i][col])>abs(a[max_r][col]))
                max_r=i;
        }
        if(max_r!=k) // 与第k行交换.
        {
            for(j=k; j<var+1; j++)
                swap(a[k][j],a[max_r][j]);
        }
        if(a[k][col]==0) // 说明该col列第k行以下全是0了,则处理当前行的下一列.
        {
            k--;
            continue;
        }
        for(i=k+1; i<equ; i++) // 枚举要删去的行.
        {
            if(a[i][col]!=0)
            {
                LCM = lcm(abs(a[i][col]),abs(a[k][col]));
                ta = LCM/abs(a[i][col]);
                tb = LCM/abs(a[k][col]);
                if(a[i][col]*a[k][col]<0)
                    tb=-tb;//异号的情况是相加
                for(j=col; j<var+1; j++)
                {
                    a[i][j] = ((a[i][j]*ta-a[k][j]*tb)%7+7)%7;// !!!
                }
            }
        }
    }
    // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
    for (i = k; i < equ; i++)  // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
    {
        if (a[i][col] != 0)
            return -1;
    }
    // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
    // 且出现的行数即为自由变元的个数.
    if (k < var)
    {
        return var - k; // 自由变元有var - k个.
    }
    // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.
    // 计算出Xn-1, Xn-2 ... X0.
    for (i = var - 1; i >= 0; i--)
    {
        temp = a[i][var];
        for (j = i + 1; j < var; j++)
        {
            if (a[i][j] != 0)
                temp = ((temp-a[i][j] * x[j])%7+7)%7;// !!!
        }
        while (temp % a[i][i] != 0) // !!! zhu yi bian hua
            temp+=7; // 说明有浮点数解,但无整数解. // !!!
        x[i] = (temp / a[i][i])%7; // !!!
    }
    return 0;
}
int b[MAXN];
map<string,int>mp;
char s[MAXN],ss[MAXN];
int main(void)
{
    mp["MON"]=1;
    mp["TUE"]=2;
    mp["WED"]=3;
    mp["THU"]=4;
    mp["FRI"]=5;
    mp["SAT"]=6;
    mp["SUN"]=7;
    int T,cas=1;
    int i, j;
    int equ,var;
    while (scanf("%d%d",&var,&equ)!=EOF&&(var||equ))
    {
        memset(a, 0, sizeof(a));
        for (i = 0; i < equ; i++)
        {
            int x,y;
            scanf("%d %s %s",&x,s,ss);
            a[i][var]=((mp[ss]-mp[s]+1)%7+7)%7; // !!!
            while(x--)
            {
                scanf("%d",&y);
                a[i][y-1]++;
                a[i][y-1]%=7; // !!!
            }
        }
        int free_num = Gauss(equ,var);
        if (free_num == -1) printf("Inconsistent data.\n");
        else if(free_num > 0) printf("Multiple solutions.\n");
        else {
            for(int i=0;i<var;i++)
            {
                if(x[i]<3) x[i]+=7;// !!! ti mu yao qiu 3~9
                printf("%d%c",x[i],i==var-1?'\n':' ');
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/88896912
今日推荐