POJ 2947 Widget Factory (高斯消元)
来源:互联网 发布:mac os x系统官方下载 编辑:程序博客网 时间:2024/06/05 11:05
Description
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
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
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.
算法目的: 高斯消元,一般用于求解线性方程组AX = B(或 模线性方程组AX mod P = B),以四个未知数,四个方程为例,AX=B表示成4x4的矩 阵和4x1的矩阵相乘的形式:
其中A和B(b0 b1 b2 b3)已知,要求列向量X(x0 x1 x2 x3)的值。
算法核心思想: 对于n个方程,m个未知数的方程组,消元的具体步骤如下:
1、枚举第i (0 <= i < n) 行,初始化列为col = 0,每次从[i, n)行中找到第col列中元素绝对值最大的行和第i行进行交换(找到最大的行是为了在消元的时候把浮点数的误差降到最小);
a) 如果第col列的元素全为0,放弃这一列的处理,col+1,i不变,转1);
b) 否则,对于所有的行j (i < j < n),如果a[j][col]不为0,则需要进行消元,以期第i行以下的第col列的所有元素都消为0(这一步就是线性代数中所说的初等行变换,具体的步骤就是将第j行的所有元素减去第i行的所有元素乘上一个系数,这个系数即a[j][col] / a[i][col])。
2、重复步骤1) 直到n个方程枚举完毕或者列col == m。
3、判断解的情况:
a) 如果出现某一行,系数矩阵全为0,增广矩阵不全为0,则无解(即出现[0 0 0 0 0 b],其中b不等于0的情况);
b) 如果是严格上三角,则表明有唯一解;
c) 如果增广矩阵有k (k > 0)行全为0,那么表明有k个变量可以任意取值,这几个变量即自由变量;对于这种情况,一般解的范围是给定的,令解的取值有T个,自由变量有V个,那么解的个数就是 TV。
n个物品,m条记录,每条记录上有完成物品的个数 和 时间
如样例
2 3
2 MON THU 1 2 3 MON FRI 1 1 2 3 MON SUN 1 2 2可列出方程x1 + x2 = 42x1 + x2 = 5x1 + 2x2 = 7直接模板求解即可,此题应注意%7#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>using namespace std;const int inf=0x3f3f3f3f;const int MAXN=310;int a[MAXN][MAXN];int m,n;int xi[MAXN];int judge(char s[]){ if(strcmp(s,"MON")==0) return 1; else if(strcmp(s,"TUE")==0) return 2; else if(strcmp(s,"WED")==0) return 3; else if(strcmp(s,"THU")==0) return 4; else if(strcmp(s,"FRI")==0) return 5; else if(strcmp(s,"SAT")==0) return 6; else return 7;}int gcd(int x,int y){ int r; while(y) { r=x%y; x=y; y=r; } return x;}int lcm(int x,int y){ return x/gcd(x,y)*y;}int Gauss(){ int row,col,i,j,maxr; for(row=0,col=0; row<m && col<n ; row++,col++) { maxr=row; for(i=row+1;i<m;i++) // 找到该col列元素绝对值最大的那行与第row行交换 { if(abs(a[maxr][col]) < abs(a[i][col])) maxr=i; } if(maxr!=row) { for(i=0; i<=n; i++) swap( a[row][i] ,a[maxr][i] ); } if(a[row][col] == 0) //说明该col列第row行以下全是0了,则处理当前行的下一列. { row--; continue; } for(i=row+1; i<m;i++) { if(a[i][col]) // 消元 第i行以下的第col列的所有元素都消为0 { int LCM = lcm(abs(a[i][col]),abs(a[row][col])); int ta = LCM / abs(a[i][col]) ; int tb = LCM / abs(a[row][col]); if(a[i][col] * a[row][col] < 0 ) tb = -tb; for(j = 0; j <=n ;j++) { a[i][j] = a[i][j]*ta - a[row][j]*tb; a[i][j] = ((a[i][j]%7+7)%7); } } } } for(i=row; i<m; i++) // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0) { if(a[i][col]) return -1; } if(row < n) return n-row; // 2. 无穷解的情况: 在n * (n + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵. // 且出现的行数即为自由变元的个数,即(0,0,...,0)的行数 for(i=n-1;i>=0;i--) //3. 唯一解的情况: 在n * (n + 1)的增广阵中形成严格的上三角阵. // 依次计算出Xn-1, Xn-2 ... X0. { int y=a[i][n]; //等式右侧的值 for( j=i+1; j<n;j++) { if(a[i][j]) { y -= a[i][j]*xi[j]; //把已知的解带入,减去,只剩下,一个未知的解 y=(y%7+7)%7; } } while(y%a[i][i]!=0) { y += 7; } xi[i] = y/a[i][i] % 7; } return 0;}int main(){ while(scanf("%d%d",&n,&m) && (m||n)) { memset(a,0,sizeof(a)); int c; char s1[10],s2[10]; for(int i=0;i<m;i++) { scanf("%d%s%s",&c,s1,s2); while(c--) { int d; scanf("%d",&d); a[i][d-1]++; } a[i][n]= ((judge(s2)-judge(s1)+1)%7+7)%7; } int num=Gauss(); if(num==-1) printf("Inconsistent data.\n"); else if(num) printf("Multiple solutions.\n"); else { for(int i=0;i<n;i++) { if(i!=n-1) { if(xi[i]<3) printf("%d ",xi[i]+7); else printf("%d ",xi[i]); } else { if(xi[i]<3) printf("%d\n",xi[i]+7); else printf("%d\n",xi[i]); } } } } return 0;}
- poj 2947 Widget Factory(高斯消元)
- [ACM] POJ 2947 Widget Factory (高斯消元)
- poj 2947 Widget Factory(数学:高斯消元)
- POJ 2947 Widget Factory (高斯消元)
- POJ 2947 Widget Factory(高斯消元)
- poj 2947 Widget Factory (高斯消元)
- poj 2947 Widget Factory(高斯消元)
- POJ 2947 Widget Factory 高斯消元
- poj -- 2947 Widget Factory(高斯消元)
- POJ 2947 Widget Factory (高斯消元)
- poj 2947 Widget Factory (高斯消元)
- Widget Factory - POJ 2947 高斯消元
- poj 2947 Widget Factory (高斯消元)
- POJ 2947 Widget Factory(高斯消元)
- POJ 2947 Widget Factory(高斯消元)
- poj 2947 Widget Factory
- POJ 2947 Widget Factory
- POJ 2947 Widget Factory
- VMware 中 Ubuntu 不能全屏
- 多终端概念
- qml自学笔记------自己写类似于劲舞团的按键小游戏(中)
- php array与object 相互转化
- 地质地貌卫星影像集锦(一 典型地貌篇)
- POJ 2947 Widget Factory (高斯消元)
- MyBatis学习系列四:接口式编程
- JAVA设计模式之单例模式
- 6.22刷题记录
- AndroidStudio实用插件整理,打造犀利神器
- RobotFramework 数据分离
- 参数传递二维数组
- addEventListener()与removeEventListener()
- 图像处理------Fuzzy C Means的聚合算法