天天看點

Widget Factory--高斯消元

Widget Factory

Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 5806 Accepted: 2013

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. 

Source

Central Europe 2005

題目連結:http://poj.org/problem?id=2947

QAQ,高斯消元早就懂,代碼快敲死我了。

此題的題意很簡單,公司被吞并,老員工幾乎全部被炒鱿魚。一共有n種不同的工具,編号1-N(代碼中是0—N-1),每種工具的加工時間為3——9天,但是現在老員工不在我們不知道每種工具的加工時間,慶幸的是還保留着一些對勞工制造工具的記錄,對于每個老員工,他的記錄包括,他開始工作的時間(在某個星期的星期幾),被炒鱿魚的時間(某個星期的星期幾),在第幾個星期不知道.....在這段時間裡,他正好加工了k件物品,給出了這k件物品的編号。我們要做的就是通過這些記錄,來确定每種工具的加工時間是多少。

高斯消元的模闆題,很容易就可以建出來增廣矩陣,自從上次宇航巨巨講了高斯消元之後我這還是來A的第一個題,慚愧慚愧,原理線代上就學了,代碼快搞死我了。

我本想寫一下題解,敲了挺多的,但是怎麼都沒有大牛的好。。。畢竟大牛,我蒟蒻,大牛的連結放這把,加油!

http://blog.csdn.net/sr_19930829/article/details/38275863

代碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int equ,var;
int a[333][333];
int x[333];
int free_num;
int lcm(int a,int b){
    return a*b/__gcd(a,b);
}
int change(char s[]){
    if(!strcmp(s,"MON"))
        return 1;
    else if(!strcmp(s,"TUE")){
        return 2;
    }
    else if(!strcmp(s,"WED")){
        return 3;
    }
    else if(!strcmp(s,"THU")){
        return 4;
    }
    else if(!strcmp(s,"FRI")){
        return 5;
    }
    else if(!strcmp(s,"SAT")){
        return 6;
    }
    else
        return 7;
}
int Gauss(){
    int mic;
    int col;
    int ta,tb;
    int LCM;
    int temp;
    int k;
    for(k=0,col=0;k<equ&&col<var;k++,col++){
        mic=k;
        for(int i=k+1;i<equ;i++){
            if(abs(a[i][col])>abs(a[mic][col]))
                mic=i;
        }
        if(mic!=k){
            for(int j=k;j<var+1;j++){
                swap(a[k][j],a[mic][j]);
            }
        }
        if(a[k][col]==0){
            k--;
            continue;
        }
        for(int 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(int j=col;j<var+1;j++){
                    a[i][j]=(((a[i][j]*ta-a[k][j]*tb)%7+7)%7);
                }
            }
        }
    }
    for(int i=k;i<equ;i++){
        if(a[i][col]!=0)
            return -1;
    }
    if(k<var){
        return var-k;
    }
    for(int i=var-1;i>=0;i--){
        temp=a[i][var];
        for(int 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 n,m,k,num;
    char s[10],e[10];
    while(~scanf("%d%d",&n,&m)){
        if(n+m==0)
            break;
        memset(a,0,sizeof(a));
        for(int i=0;i<m;i++){
            scanf("%d",&k);
            scanf("%s%s",s,e);
            a[i][n]=((change(e)-change(s)+1)%7+7)%7;
            for(int j=1;j<=k;j++){
                scanf("%d",&num);
                num--;
                a[i][num]++;
                a[i][num]%=7;
            }
        }
        equ=m;
        var=n;
        free_num=Gauss();
        if(free_num==0){
            for(int i=0;i<n;i++){
                if(x[i]<=2)
                    x[i]+=7;
            }
            for(int i=0;i<n;i++){
                printf(i==0?"%d":" %d",x[i]);
            }
            cout<<endl;
        }
        else if(free_num==-1){
            cout<<"Inconsistent data."<<endl;
        }
        else{
            cout<<"Multiple solutions."<<endl;
        }
    }
    return 0;
}