您的位置:首页 > 其它

poj_2947 Widget Factory(高斯消元求解模线性方程)

2016-12-19 16:26 447 查看
Widget Factory

Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 5819 Accepted: 2018
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.  

有n种器具,m个已知条件,
每个条件中给出生产器具的 起始时间与结束时间(以星期几的方式给出),生产的器具总数和它们各是哪一种器具。
求生产每种器具所需天数,规定天数范围为3~9。
------------------------------------------------
设生产一个器具i所需天数为Xi,每个条件中生产器具i的个数为ai,生产总天数为S。
可得:a1*X1+a2*X2+……+an*Xn = S
只是题目生产所需时间是以星期几的方式给出,即我们只知道S(mod7)的值,所以要对方程两边对7取模:
(a1*X1+a2*X2+……+an*Xn)mod7 = S (mod7)
这样我们就得到了一系列的模线性方程,可用高斯消元求解。
注意规定天数为3~9,所以当我们求到0~2的解时记得加上7。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf 0x3f3f3f3f
#define maxn 310
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

typedef int Matrix[maxn][maxn];
int x[maxn];
int n, m;

int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a%b);
}

int lcm(int a, int b)
{
return a / gcd(a, b) * b; //防溢出
}

//equ个方程,var个变元 A[r][c], 0<r<equ, 0<c<var
int gauss_elimination(Matrix A, int equ, int var)
{
memset(x, 0, sizeof(x));
int row, col; //当前处理的行row和列col
int max_r;
for(row = 0, col = 0; row < equ && col < var; row++, col++)
{
//找到该col列元素绝对值最大的那行与第row行交换,提高数值稳定性
max_r = row;
for(int r = row+1; r < equ; r++)
{
if(abs(A[r][col]) > abs(A[max_r][col])) max_r = r;
}
if(max_r != row) for(int c = 0; c <= var; c++) swap(A[max_r][c], A[row][c]);

if(A[row][col] == 0) { row--; continue; } //说明该col列第row行以下全是0了,则处理当前行的下一列.

//与第row+1~equ行进行消元
int LCM, ta, tb;
for(int r = row+1; r < equ; r++)
{
if(A[r][col] == 0) continue; //一开始忘记加这一行了,导致RE了几次。

LCM = lcm(abs(A[r][col]),abs(A[row][col]));
ta = LCM / abs(A[r][col]);
tb = LCM / abs(A[row][col]);
if(A[r][col] * A[row][col] < 0) tb = -tb;//异号的情况是相加
for(int c = col; c <= var; c++) A[r][c] = ((A[r][c] * ta - A[row][c] * tb)%7 + 7)%7;
}
}

// 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0),即矛盾方程
for (int r = row; r < equ; r++) if (A[r][col] != 0) return -1;

// 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
// row表示有row个不是矛盾方程的非0行,即有row个非自由变量,总变量 - 非自由变量数 = 自由变量数。
if(row < var) return var - row;

// 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵。
// 计算出Xn-1, Xn-2 ... X0.
int temp;
for (int r = var - 1; r >= 0; r--)
{
temp = A[r][var];
for(int c = r + 1; c < var; c++)
{
if (A[r][c] != 0) temp -= x[c] * A[r][c];
temp = (temp%7+7)%7;
}
//if (temp % A[r][r] != 0) return -2; //说明有浮点数解,但无整数解.
while(temp % A[r][r] != 0) temp += 7; //剩余系的特殊处理
x[r] = (temp / A[r][r])%7;
if(x[r] <= 2) x[r] += 7;
4000
}
return 0;

}

int weekday(char *s)
{
if(strcmp(s, "MON")==0) return 1;
if(strcmp(s, "TUE")==0) return 2;
if(strcmp(s, "WED")==0) return 3;
if(strcmp(s, "THU")==0) return 4;
if(strcmp(s, "FRI")==0) return 5;
if(strcmp(s, "SAT")==0) return 6;
if(strcmp(s, "SUN")==0) return 7;
}

Matrix A;

int main()
{
char s[5], e[5];
while(~scanf("%d%d", &n, &m) && (n || m))
{
memset(A, 0, sizeof(A));

int p, a; //a表示第a种器具
for(int i = 0; i < m; i++)
{
scanf("%d%s%s", &p, s, e);
A[i]
= ((weekday(e)-weekday(s)+1)+7)%7;
for(int j = 1; j <= p; j++)
{
scanf("%d", &a);
A[i][a-1] = (A[i][a-1]+1)%7;
}
}
int ans = gauss_elimination(A, m, n);
if(ans == 0)
{
for(int i = 0; i < n; i++)
{
if(i != n-1) printf("%d ", x[i]);
else printf("%d\n", x[i]);
}
}
else if(ans == -1) printf("Inconsistent data.\n");
else printf("Multiple solutions.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: