Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

3735一直WA啊,求求大神帮忙QAQ 报个TLE处理稀疏矩阵也好啊,但一直WA

Posted by ljk8800 at 2015-04-17 22:37:11
import java.util.Scanner;

public class Main{
	static int N;
	static int M;
	static int K;
	static int[][] cats;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		M = scan.nextInt();
		K = scan.nextInt();
		while (N != 0 || M != 0 || K != 0) {
			cats = new int[N + 1][N + 1];
			for (int i = 0; i < cats.length; i++) {
				cats[i][i] = 1;
			}
			for (int i = 0; i < K; i++) {
				String action = scan.next();
				if ("g".equals(action)) {
					cats[scan.nextInt() - 1][N]++;
				} else if ("s".equals(action)) {
					int a = scan.nextInt() - 1;
					int b = scan.nextInt() - 1;
					int[] temp = cats[a];
					cats[a] = cats[b];
					cats[b] = temp;
				} else if ("e".equals(action)) {
					int index = scan.nextInt() - 1;
					for (int j = 0; j < cats[index].length; j++) {
						cats[index][j] = 0;
					}
				}
			}
			solve();
			N = scan.nextInt();
			M = scan.nextInt();
			K = scan.nextInt();
		}
		scan.close();
	}

	private static void solve() {
		cats = pow(cats, M);
		for (int i = 0; i < N - 1; i++) {
			System.out.print(cats[i][N] + " ");
		}
		System.out.println(cats[N - 1][N]);
	}

	private static int[][] mul(int[][] matA, int[][] matB) {
		int[][] matrix = new int[matA.length][matB[0].length];
		for (int i = 0; i < matA.length; i++) {
			for (int j = 0; j < matB[0].length; j++) {
				for (int k = 0; k < matA[0].length; k++) {
					matrix[i][j] += matA[i][k] * matB[k][j];
				}
			}
		}

		return matrix;
	}

	private static int[][] pow(int[][] mat, int n) {
		int[][] matrix = new int[mat.length][mat[0].length];
		for (int i = 0; i < matrix.length; i++) {
			matrix[i][i] = 1;
		}
		while (n > 0) {
			if ((n & 1) == 1) {
				matrix = mul(mat, matrix);
			}
			mat = mul(mat, mat);
			n >>= 1;
		}
		return matrix;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator