POJ 2947 高斯消元

//Result:wizmann	2947	Accepted	1144K	1813MS	G++	2291B

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <iostream>

#include <algorithm>

#include <cmath>

#include <map>



using namespace std;



#define print(x) cout<<x<<endl

#define input(x) cin>>x

#define SIZE 320

#define BUF 30

#define WEEK 7



map<string,int> date;



void init()

{

	date["MON"]=0;

	date["TUE"]=1;

	date["WED"]=2;

	date["THU"]=3;

	date["FRI"]=4;

	date["SAT"]=5;

	date["SUN"]=6;

}



int n,m;

int matrix[SIZE][SIZE];



int gcd(int a,int b)

{

	if(b>a) return gcd(b,a);

	else if(a%b==0) return b;

	else return gcd(b,a%b);

}



inline int lcm(int a,int b)

{

	return a*b/gcd(a,b);

}



int gauss()

{

	int row,col;

	for(row=0,col=0;row<m&&col<n;row++,col++)

	{

		int k=row;

		for(int i=row+1;i<m;i++)

		{

			if(abs(matrix[k][col])<abs(matrix[i][col])) k=i;

		}

		if(k!=row)

		{

			for(int i=col;i<=n;i++)

			{

				swap(matrix[row][i],matrix[k][i]);

			}

		}

		if(!matrix[row][col])

		{

			row--;

			continue;

		}

	

		for(int i=row+1;i<m;i++)

		{

			if(matrix[i][col])

			{

				int LCM=lcm(abs(matrix[i][col]),abs(matrix[row][col]));

				int a=LCM/matrix[i][col];

				int b=LCM/matrix[row][col];

				for(int j=col;j<=n;j++)

				{

					matrix[i][j]=(matrix[i][j]*a-matrix[row][j]*b)%WEEK;

				}

			}

		}

	}

	for(int i=row;i<m;i++)

	{

		if(matrix[i][n]) return -1;

	}

	if(row<n) return 1;

	

	int x[SIZE]={0};

	for(int i=n-1;i>=0;i--)

	{

		int tp=matrix[i][n];

		for(int j=i+1;j<n;j++)

		{

			tp-=matrix[i][j]*x[j];

		}

		while(tp%matrix[i][i]) tp+=WEEK;



		x[i]=tp/matrix[i][i];

		while(x[i]<3) x[i]+=WEEK;

		while(x[i]>9) x[i]-=WEEK;

	}

	for(int i=0;i<n-1;i++)

	{

		printf("%d ",x[i]);

	}

	printf("%d\n",x[n-1]);

	return 0;

}



int main()

{

	init();

	int a;

	char start[BUF],end[BUF];

	while(scanf("%d%d",&n,&m)!=EOF && n+m)

	{

		memset(matrix,0,sizeof(matrix));

		for(int i=0;i<m;i++)

		{

			int k;

			scanf("%d",&k);

			scanf("%s%s",start,end);

			int day=date[end]-date[start];

			day=(day%WEEK+WEEK+1)%WEEK;

			matrix[i][n]=day;

			for(int j=0;j<k;j++)

			{

				scanf("%d",&a);

				matrix[i][a-1]++;

				matrix[i][a-1]%=WEEK;

			}

		}

		int res=gauss();

		if(res==1) puts("Multiple solutions.");

		else if(res==-1) puts("Inconsistent data.");

	}

	return 0;

}

  

//Result:wizmann2947Accepted1144K1813MSG++2291B#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <map>
using namespace std;
#define print(x) cout<<x<<endl#define input(x) cin>>x#define SIZE 320#define BUF 30#define WEEK 7
map<string,int> date;
void init(){date["MON"]=0;date["TUE"]=1;date["WED"]=2;date["THU"]=3;date["FRI"]=4;date["SAT"]=5;date["SUN"]=6;}
int n,m;int matrix[SIZE][SIZE];
int gcd(int a,int b){if(b>a) return gcd(b,a);else if(a%b==0) return b;else return gcd(b,a%b);}
inline int lcm(int a,int b){return a*b/gcd(a,b);}
int gauss(){int row,col;for(row=0,col=0;row<m&&col<n;row++,col++){int k=row;for(int i=row+1;i<m;i++){if(abs(matrix[k][col])<abs(matrix[i][col])) k=i;}if(k!=row){for(int i=col;i<=n;i++){swap(matrix[row][i],matrix[k][i]);}}if(!matrix[row][col]){row--;continue;}for(int i=row+1;i<m;i++){if(matrix[i][col]){int LCM=lcm(abs(matrix[i][col]),abs(matrix[row][col]));int a=LCM/matrix[i][col];int b=LCM/matrix[row][col];for(int j=col;j<=n;j++){matrix[i][j]=(matrix[i][j]*a-matrix[row][j]*b)%WEEK;}}}}for(int i=row;i<m;i++){if(matrix[i][n]) return -1;}if(row<n) return 1;int x[SIZE]={0};for(int i=n-1;i>=0;i--){int tp=matrix[i][n];for(int j=i+1;j<n;j++){tp-=matrix[i][j]*x[j];}while(tp%matrix[i][i]) tp+=WEEK;
x[i]=tp/matrix[i][i];while(x[i]<3) x[i]+=WEEK;while(x[i]>9) x[i]-=WEEK;}for(int i=0;i<n-1;i++){printf("%d ",x[i]);}printf("%d\n",x[n-1]);return 0;}
int main(){freopen("input.txt","r",stdin);init();int a;char start[BUF],end[BUF];while(scanf("%d%d",&n,&m)!=EOF && n+m){memset(matrix,0,sizeof(matrix));for(int i=0;i<m;i++){int k;scanf("%d",&k);scanf("%s%s",start,end);int day=date[end]-date[start];day=(day%WEEK+WEEK+1)%WEEK;matrix[i][n]=day;for(int j=0;j<k;j++){scanf("%d",&a);matrix[i][a-1]++;matrix[i][a-1]%=WEEK;}}int res=gauss();if(res==1) puts("Multiple solutions.");else if(res==-1) puts("Inconsistent data.");}return 0;}

 

你可能感兴趣的:(poj)