POJ 2947 Widget Factory (高斯消元)

来源:互联网 发布:mac os x系统官方下载 编辑:程序博客网 时间:2024/06/05 11:05
Widget Factory
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 4944 Accepted: 1699

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 32 MON THU1 23 MON FRI1 1 23 MON SUN1 2 210 21 MON TUE 31 MON WED30 0

Sample Output

8 3Inconsistent data.

算法目的:        高斯消元,一般用于求解线性方程组AX = B(或 模线性方程组AX mod P = B),以四个未知数,四个方程为例,AX=B表示成4x4的矩        阵和4x1的矩阵相乘的形式:  

其中A和B(b0 b1 b2 b3)已知,要求列向量X(x0 x1 x2 x3)的值。

算法核心思想:            对于n个方程,m个未知数的方程组,消元的具体步骤如下:

1、枚举第i (0 <= i < n) 行,初始化列为col = 0,每次从[i, n)行中找到第col列中元素绝对值最大的行和第i行进行交换(找到最大的行是为了在消元的时候把浮点数的误差降到最小);

a) 如果第col列的元素全为0,放弃这一列的处理,col+1,i不变,转1);

b) 否则,对于所有的行j (i < j < n),如果a[j][col]不为0,则需要进行消元,以期第i行以下的第col列的所有元素都消为0(这一步就是线性代数中所说的初等行变换,具体的步骤就是将第j行的所有元素减去第i行的所有元素乘上一个系数,这个系数即a[j][col] / a[i][col])。

2、重复步骤1) 直到n个方程枚举完毕或者列col == m。

3、判断解的情况:

a) 如果出现某一行,系数矩阵全为0,增广矩阵不全为0,则无解(即出现[0 0 0 0 0 b],其中b不等于0的情况);

b) 如果是严格上三角,则表明有唯一解;

c) 如果增广矩阵有k (k > 0)行全为0,那么表明有k个变量可以任意取值,这几个变量即自由变量;对于这种情况,一般解的范围是给定的,令解的取值有T个,自由变量有V个,那么解的个数就是 TV。

n个物品,m条记录,每条记录上有完成物品的个数 和 时间

如样例

2 3

2 MON THU   1 2   3 MON FRI   1 1 2   3 MON SUN   1 2 2
可列出方程
x1 + x2 = 4
2x1 + x2 = 5
x1 + 2x2 = 7
直接模板求解即可,此题应注意%7
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>using namespace std;const int inf=0x3f3f3f3f;const int MAXN=310;int a[MAXN][MAXN];int m,n;int xi[MAXN];int judge(char s[]){    if(strcmp(s,"MON")==0) return 1;    else if(strcmp(s,"TUE")==0) return 2;    else if(strcmp(s,"WED")==0) return 3;    else if(strcmp(s,"THU")==0) return 4;    else if(strcmp(s,"FRI")==0) return 5;    else if(strcmp(s,"SAT")==0) return 6;    else return 7;}int gcd(int x,int y){    int r;    while(y)    {        r=x%y;         x=y;         y=r;    }    return x;}int lcm(int x,int y){    return x/gcd(x,y)*y;}int Gauss(){    int row,col,i,j,maxr;    for(row=0,col=0; row<m && col<n ; row++,col++)    {        maxr=row;        for(i=row+1;i<m;i++)     // 找到该col列元素绝对值最大的那行与第row行交换        {            if(abs(a[maxr][col]) < abs(a[i][col]))                maxr=i;        }        if(maxr!=row)                    {        for(i=0; i<=n; i++)            swap( a[row][i] ,a[maxr][i] );        }        if(a[row][col] == 0)    //说明该col列第row行以下全是0了,则处理当前行的下一列.         {            row--;            continue;        }        for(i=row+1; i<m;i++)            {            if(a[i][col])     // 消元 第i行以下的第col列的所有元素都消为0                  {                int LCM = lcm(abs(a[i][col]),abs(a[row][col]));                int ta = LCM / abs(a[i][col]) ;                int tb = LCM / abs(a[row][col]);                if(a[i][col] * a[row][col] < 0 ) tb = -tb;                for(j = 0; j <=n ;j++)                {                    a[i][j] = a[i][j]*ta - a[row][j]*tb;                    a[i][j] = ((a[i][j]%7+7)%7);                }            }        }    }    for(i=row; i<m; i++)     // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0)    {        if(a[i][col]) return -1;    }    if(row < n) return n-row;    // 2. 无穷解的情况: 在n * (n + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.                                  // 且出现的行数即为自由变元的个数,即(0,0,...,0)的行数     for(i=n-1;i>=0;i--)       //3. 唯一解的情况: 在n * (n + 1)的增广阵中形成严格的上三角阵.                               // 依次计算出Xn-1, Xn-2 ... X0.      {        int y=a[i][n];        //等式右侧的值        for( j=i+1; j<n;j++)        {            if(a[i][j])            {                y -= a[i][j]*xi[j];   //把已知的解带入,减去,只剩下,一个未知的解                 y=(y%7+7)%7;            }        }        while(y%a[i][i]!=0)        {            y += 7;        }        xi[i] = y/a[i][i] % 7;    }    return 0;}int main(){    while(scanf("%d%d",&n,&m) && (m||n))    {        memset(a,0,sizeof(a));        int c;        char s1[10],s2[10];        for(int i=0;i<m;i++)        {        scanf("%d%s%s",&c,s1,s2);        while(c--)        {            int d;            scanf("%d",&d);            a[i][d-1]++;        }       a[i][n]= ((judge(s2)-judge(s1)+1)%7+7)%7;        }        int num=Gauss();        if(num==-1)         printf("Inconsistent data.\n");        else if(num)            printf("Multiple solutions.\n");        else        {            for(int i=0;i<n;i++)            {               if(i!=n-1)                {                    if(xi[i]<3)                        printf("%d ",xi[i]+7);                    else  printf("%d ",xi[i]);                }                else                {                     if(xi[i]<3)                        printf("%d\n",xi[i]+7);                    else  printf("%d\n",xi[i]);                }            }        }    }    return 0;}




0 0
原创粉丝点击