HDU 5671 Matrix (矩阵操作)

Matrix 


 Time Limit: 3000/1500 MS (Java/Others)   Memory Limit: 131072/131072 K (Java/Others)


Problem Description
There is a matrix MM that has nn rows and mm columns (1 \leq n \leq 1000 ,1 \leq m \leq 1000 )(1≤n≤1000,1≤m≤1000).Then we perform q (1 \leq q \leq 100,000)q(1≤q≤100,000) operations:


1 x y: Swap row x and row y (1 \leq x,y \leq n)(1≤x,y≤n);


2 x y: Swap column x and column y (1 \leq x,y \leq m)(1≤x,y≤m);


3 x y: Add y to all elements in row x (1 \leq x \leq n,1 \leq y \leq 10,000)(1≤x≤n,1≤y≤10,000);


4 x y: Add y to all elements in column x (1 \leq x \leq m,1 \leq y \leq 10,000)(1≤x≤m,1≤y≤10,000);


Input
There are multiple test cases. The first line of input contains an integer T (1\leq T\leq 20)T(1≤T≤20) indicating the number of test cases. For each test case:


The first line contains three integers nn, mm and qq. The following nn lines describe the matrix M.(1 \leq M_{i,j} \leq 10,000)(1≤M
​i,j
​​ ≤10,000) for all (1 \leq i \leq n,1 \leq j \leq m)(1≤i≤n,1≤j≤m). The following qq lines contains three integers a(1 \leq a \leq 4)a(1≤a≤4), xx and yy.


Output
For each test case, output the matrix MM after all qq operations.


Sample Input
Copy
2
3 4 2
1 2 3 4
2 3 4 5
3 4 5 6
1 1 2
3 1 10
2 2 2
1 10
10 1
1 1 2
2 1 2
Sample Output
12 13 14 15
1 2 3 4
3 4 5 6
1 10
10 1


Hint
 Recommand to use scanf and printf 

Problem Description
There is a matrix MM that has nn rows and mm columns (1 \leq n \leq 1000 ,1 \leq m \leq 1000 )(1≤n≤1000,1≤m≤1000).Then we perform q (1 \leq q \leq 100,000)q(1≤q≤100,000) operations:


1 x y: Swap row x and row y (1 \leq x,y \leq n)(1≤x,y≤n);


2 x y: Swap column x and column y (1 \leq x,y \leq m)(1≤x,y≤m);


3 x y: Add y to all elements in row x (1 \leq x \leq n,1 \leq y \leq 10,000)(1≤x≤n,1≤y≤10,000);


4 x y: Add y to all elements in column x (1 \leq x \leq m,1 \leq y \leq 10,000)(1≤x≤m,1≤y≤10,000);


Input
There are multiple test cases. The first line of input contains an integer T (1\leq T\leq 20)T(1≤T≤20) indicating the number of test cases. For each test case:


The first line contains three integers nn, mm and qq. The following nn lines describe the matrix M.(1 \leq M_{i,j} \leq 10,000)(1≤M
​i,j
​​ ≤10,000) for all (1 \leq i \leq n,1 \leq j \leq m)(1≤i≤n,1≤j≤m). The following qq lines contains three integers a(1 \leq a \leq 4)a(1≤a≤4), xx and yy.


Output
For each test case, output the matrix MM after all qq operations.


Sample Input
Copy
2
3 4 2
1 2 3 4
2 3 4 5
3 4 5 6
1 1 2
3 1 10
2 2 2
1 10
10 1
1 1 2

2 1 2


Sample Output
12 13 14 15
1 2 3 4
3 4 5 6
1 10
10 1


Hint

 Recommand to use scanf and printf 


题解思路:

对于交换行、交换列的操作,分别记录当前状态下每一行、每一列是原始数组的哪一行、哪一列即可。

对每一行、每一列加一个数的操作,也可以两个数组分别记录。注意当交换行、列的同时,也要交换增量数组。

输出时通过索引找到原矩阵中的值,再加上行、列的增量。

复杂度O(q+mn)O(q+mn)




超时TLE代码(当天比赛的时候提交还过了,比赛后又重判了,TLE ,看样子比赛的时候的数据水啊)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

long long int Map[1100][1100];
int n,m;

void calculate1(int x,int y)
{
	for(int i = 0;i < m;i++)
	{
		swap(Map[x][i],Map[y][i]);
	}
}

void calculate2(int x,int y)
{
	for(int i = 0;i < n;i++)
	{
		swap(Map[i][x],Map[i][y]);
	}
}

void calculate3(int x,int y)
{
	for(int i = 0;i < m;i++)
	{
		Map[x][i] += y;
	}
}

void calculate4(int x,int y)
{
	for(int i = 0;i < n;i++)
	{
		Map[i][x] += y;
	}
}


int main()
{
	int p,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&n,&m,&p);
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
				scanf("%I64d",&Map[i][j]);
			}
		}
		int a;
		int x,y;
		
		while(p--)
		{
			scanf("%d%d%d",&a,&x,&y);
			switch(a)
			{
			case 1 : calculate1(x-1,y-1);break;
			case 2 : calculate2(x-1,y-1);break;
			case 3 : calculate3(x-1,y);break;
			case 4 : calculate4(x-1,y);break;
			}
		}
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
				if(j != 0)
				printf(" ");
				printf("%I64d",Map[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
} 


今天又重新写了一遍,AC代码

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;

long long int  Map[1100][1100],row[1100],col[1100];   //  row[x] 表示 应该在第 x 行的在第 row[x] 行 ,col同理表示列
int n,m;

void Init()
{
	for(int i = 0;i < 1100;i++)
	{
		row[i] = i;
		col[i] = i;
	}
}

void calculate3(int x,int y)
{
	for(int i = 0;i < m;i++)
	{
		Map[x][i] += y;
	}
}

void calculate4(int x,int y)
{
	for(int i = 0;i < n;i++)
	{
		Map[i][x] += y;
	}
}


int main()
{
	int p,t;
	scanf("%d",&t);
	while(t--)
	{
		Init();           //   初始化 ,最初的时候,都对应自己的行自己的列
		scanf("%d%d%d",&n,&m,&p);
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
				scanf("%I64d",&Map[i][j]);
			}
		}
		int a;
		int x,y;
		while(p--)
		{
			scanf("%d%d%d",&a,&x,&y);
			switch(a)
			{
			case 1 : swap(row[x-1],row[y-1]);break; //  交换行的时候,只把标记改下就行
			case 2 : swap(col[x-1],col[y-1]);break;
			case 3 : calculate3(row[x-1],y);break;
			case 4 : calculate4(col[x-1],y);break;
			}
		}
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
				if(j != 0)
				printf(" ");
				printf("%I64d",Map[row[i]][col[j]]);
			}
			printf("\n");
		}
	}
	return 0;
} 


### HDU 2544 题目分析 HDU 2544 是关于最短路径的经典问题,可以通过多种方法解决,其中包括基于邻接矩阵的 Floyd-Warshall 算法。以下是针对该问题的具体解答。 --- #### 基于邻接矩阵的 Floyd-Warshall 实现 Floyd-Warshall 算法是一种动态规划算法,适用于计算任意两点之间的最短路径。它的时间复杂度为 \( O(V^3) \),其中 \( V \) 表示节点的数量。对于本题中的数据规模 (\( N \leq 100 \)),此算法完全适用。 下面是具体的实现方式: ```cpp #include <iostream> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; int dist[105][105]; int n, m; void floyd() { for (int k = 1; k <= n; ++k) { // 中间节点 for (int i = 1; i <= n; ++i) { // 起始节点 for (int j = 1; j <= n; ++j) { // 结束节点 if (dist[i][k] != INF && dist[k][j] != INF) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } } } int main() { while (cin >> n >> m && (n || m)) { // 初始化邻接矩阵 for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == j) dist[i][j] = 0; else dist[i][j] = INF; } } // 输入边的信息并更新邻接矩阵 for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; dist[u][v] = min(dist[u][v], w); dist[v][u] = min(dist[v][u], w); // 如果是有向图,则去掉这一行 } // 执行 Floyd-Warshall 算法 floyd(); // 输出起点到终点的最短距离 cout << (dist[1][n] >= INF ? -1 : dist[1][n]) << endl; } return 0; } ``` --- #### 关键点解析 1. **邻接矩阵初始化** 使用二维数组 `dist` 存储每一对节点间的最小距离。初始状态下,设所有节点对的距离为无穷大 (`INF`),而同一节点自身的距离为零[^4]。 2. **输入处理** 对于每条边 `(u, v)` 和权重 `w`,将其存储至邻接矩阵中,并取较小值以防止重边的影响[^4]。 3. **核心逻辑** Floyd-Warshall 的核心在于三重循环:依次尝试通过中间节点优化其他两节点间的距离关系。具体而言,若从节点 \( i \) 到 \( j \) 可经由 \( k \) 达成更优解,则更新对应位置的值[^4]。 4. **边界条件** 若最终得到的结果仍为无穷大(即无法连通),则返回 `-1`;否则输出实际距离[^4]。 --- #### 性能评估 由于题目限定 \( N \leq 100 \),因此 \( O(N^3) \) 的时间复杂度完全可以接受。此外,空间需求也较低,适合此类场景下的应用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值