Widget Factory--高斯消元

来源:互联网 发布:连续梁弯矩计算软件 编辑:程序博客网 时间:2024/06/05 04:02

Widget Factory
Time Limit: 7000MS Memory Limit: 65536KTotal 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 32 MON THU1 23 MON FRI1 1 23 MON SUN1 2 210 21 MON TUE 31 MON WED30 0

Sample Output

8 3Inconsistent 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;}



0 0
原创粉丝点击