POJ Widget Factory 【求解模线性方程】

传送门:http://poj.org/problem?id=2947

Widget Factory

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 7109   Accepted: 2496

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

题意概括:

给N种零件,M次工人的工作记录,每次记录包括 该工人处理了的零件数 、星期几开始 和 星期几结束(可能相隔很多个星期)。求制造每种零件所需要的时间。

解题思路:

N种零件就是N个未知数,M次操作就是M个方程。

根据M次操作构造出增广矩阵,高斯消元,不过求解过程要加上取模操作;

最后输出答案是特判一下答案是否小于等于2,如果是需要+7,因为我们运算的时候是取模运算,根据题意可知零件加工天数范围在【3,9】;

AC code:

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 #define INF 0x3f3f3f3f
  7 using namespace std;
  8 const int MAXN = 333;
  9 
 10 int a[MAXN][MAXN];//增广矩阵
 11 int x[MAXN];//解集
 12 bool free_x[MAXN];//标记是否是不确定的变元
 13 
 14 inline int gcd(int a,int b)                 //最大公约数
 15 {
 16     int t;
 17     while(b!=0)
 18     {
 19         t=b;
 20         b=a%b;
 21         a=t;
 22     }
 23     return a;
 24 }
 25 
 26 inline int lcm(int a,int b)                 //最小公倍数
 27 {
 28     return a/gcd(a,b)*b;                    //先除后乘防溢出
 29 }
 30 
 31 int Gauss(int equ,int var)
 32 {
 33     int i,j,k;
 34     int max_r;// 当前这列绝对值最大的行.
 35     int col;//当前处理的列
 36     int ta,tb;
 37     int LCM;
 38     int temp;
 39     int free_x_num;
 40     int free_index;
 41 
 42     for(int i=0;i<=var;i++)
 43     {
 44         x[i]=0;
 45         free_x[i]=true;
 46     }
 47 
 48     //转换为阶梯阵.
 49     col=0; // 当前处理的列
 50     for(k = 0;k < equ && col < var;k++,col++)
 51     {// 枚举当前处理的行.
 52 // 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)
 53         max_r=k;
 54         for(i=k+1;i<equ;i++)
 55         {
 56             if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
 57         }
 58         if(max_r!=k)
 59         {// 与第k行交换.
 60             for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);
 61         }
 62         if(a[k][col]==0)
 63         {// 说明该col列第k行以下全是0了,则处理当前行的下一列.
 64             k--;
 65             continue;
 66         }
 67         for(i=k+1;i<equ;i++)
 68         {// 枚举要删去的行.
 69             if(a[i][col]!=0)
 70             {
 71                 LCM = lcm(abs(a[i][col]),abs(a[k][col]));
 72                 ta = LCM/abs(a[i][col]);
 73                 tb = LCM/abs(a[k][col]);
 74                 if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加
 75                 for(j=col;j<var+1;j++)
 76                 {
 77                     a[i][j] = ((a[i][j]*ta-a[k][j]*tb)%7+7)%7;
 78                 }
 79             }
 80         }
 81     }
 82 
 83     // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
 84     for (i = k; i < equ; i++)
 85     { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
 86         if ( a[i][col]  != 0) return -1;
 87     }
 88 
 89     if (k < var)
 90     {
 91         return var - k; // 自由变元有var - k个.
 92     }
 93     // 3. 唯一解的情况
 94     // 计算出Xn-1, Xn-2 ... X0.
 95     for (i = var - 1; i >= 0; i--)
 96     {
 97         temp = a[i][var];
 98         for (j = i + 1; j < var; j++)
 99         {
100             if (a[i][j] != 0) temp -= a[i][j] * x[j];
101             temp=(temp%7+7)%7;
102         }
103         while (temp % a[i][i] != 0) temp+=7;
104         x[i] =( temp / a[i][i])%7 ;
105     }
106     return 0;
107 }
108 
109 int sts(char *s)
110 {
111     if(strcmp(s, "MON") == 0) return 1;
112     else if(strcmp(s, "TUE") == 0) return 2;
113     else if(strcmp(s, "WED") == 0) return 3;
114     else if(strcmp(s, "THU") == 0) return 4;
115     else if(strcmp(s, "FRI") == 0) return 5;
116     else if(strcmp(s, "SAT") == 0) return 6;
117     else if(strcmp(s, "SUN") == 0) return 7;
118 }
119 
120 int main()
121 {
122     int N, M, xx, t;
123     char str1[10], str2[10];
124     while(~scanf("%d%d", &N, &M) && (N+M)){
125         memset(a, 0, sizeof(a));
126         for(int i = 0; i < M; i++){
127             scanf("%d%s%s", &xx, str1, str2);
128             a[i][N] = ((sts(str2)-sts(str1)+1)%7+7)%7;
129 
130             for(int j = 0; j < xx; j++){
131                 scanf("%d", &t);
132                 t--;
133                 a[i][t]++;
134                 a[i][t] = a[i][t]%7;
135             }
136         }
137         int ans = Gauss(M, N);
138         if(ans == -1) puts("Inconsistent data.");
139         else if(ans == 0){
140             for(int i = 0; i < N-1; i++){
141                 if(x[i] <= 2) printf("%d ", x[i]+7);
142                 else printf("%d ", x[i]);
143             }
144             if(x[N-1] <= 2) printf("%d
", x[N-1]+7);
145             else printf("%d
", x[N-1]);
146         }
147         else{
148             puts("Multiple solutions.");
149         }
150     }
151     return 0;
152 }
View Code
原文地址:https://www.cnblogs.com/ymzjj/p/9980451.html