1. 程式人生 > >POJ2947 TOJ1799: Widget Factory 高斯消元

POJ2947 TOJ1799: Widget Factory 高斯消元

描述

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 different 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. 

輸入

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 by the 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 different 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 .

輸出

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).

樣例輸入

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

樣例輸出

8 3
Inconsistent data.

提示

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

題意:有 n 種部件,有 m 個工人,在開始工作直至停止工作的時間中完成了 k 個部件的製作,且給出這名工人所完成的部件的種類。求每一個部件製作所需要的時間。

思路:高斯消元法解模線性方程組,需考慮無解/多解/唯一解。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
const int MAXN=500;
int a[MAXN][MAXN];//增廣矩陣
int x[MAXN],amount[MAXN];//解集
char str1[4],str2[4];
map<string,int>mm;
int gcd(int a,int b)
{
    int t;
    while(b!=0)
    {
        t=b;
        b=a%b;
        a=t;
    }
    return a;
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
int Gauss(int equ,int var)
{
    int i,j,k,max_r;// 當前這列絕對值最大的行.
    int col;//當前處理的列
    int temp,ta,tb,LCM;
    int free_x_num,free_index;
    col=0; 
    for(k=0;k<equ&&col<var;k++,col++)
    {
        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)
        {
            for(j=k;j<var+1;j++) 
			swap(a[k][j],a[max_r][j]);
        }
        if(a[k][col]==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;
                }
            }
        }
    }
    for (i=k;i<equ;i++)
    {
        if(a[i][col]!=0) return -1;
    }
    if(k<var)
    {
    	return 1;
    }
    for(i=var-1;i>=0;i--)
    {
        temp=a[i][var];
        for(j=i+1;j<var;j++)
        {
            if (a[i][j]!=0) 
			temp-=a[i][j]*x[j]; 
        }
        temp=((temp%7)+7)%7;
        while(temp%a[i][i]!=0) temp+=7;
        x[i]=(temp/a[i][i])%7;
    }
    return 0;
}
int main()
{
    int i,j,n,total,num,kind;
    mm["MON"]=1,mm["TUE"]=2,mm["WED"]=3,mm["THU"]=4,mm["FRI"]=5,mm["SAT"]=6,mm["SUN"]=7;
    while(scanf("%d%d",&n,&total)!=EOF)
    {
    	if(n==0&&total==0)break;
        memset(a,0,sizeof(a));
        memset(x,0,sizeof(x));
        for(i=0;i<total;i++)
        {
            scanf("%d %s %s",&num,str1,str2);
            a[i][n]=((mm[str2]-mm[str1]+1)%7+7)%7;
            memset(amount,0,sizeof(amount));
            while(num--)
            {
                scanf("%d",&kind);
                amount[kind]++;
            }
            for(j=0;j<n;j++)
            {
                a[i][j]=amount[j+1]%7;
            }
        }
        int free_num=Gauss(total,n);
        if(free_num==-1) 
		printf("Inconsistent data.\n");
        else if(free_num==1)
        printf("Multiple solutions.\n");
        else
        {
            for(i=0;i<n;i++)
                if(x[i]<=2) x[i]+=7;
            for(i=0;i<n-1;i++)
                printf("%d ",x[i]);
            printf("%d\n",x[n-1]);
        }
    }
    return 0;
}