编程之美2014资格赛题目3:格格取数
2014-04-13 21:30
531 查看
编程之美2014资格赛题目3:格格取数
时间限制:2000ms单点时限:1000ms
内存限制:256MB
描述
给你一个m x n (1 <= m, n <= 100)的矩阵A (0<=aij<=10000),要求在矩阵中选择一些数,要求每一行,每一列都至少选到了一个数,使得选出的数的和尽量的小。
输入
多组测试数据。首先是数据组数T对于每组测试数据,第1行是两个正整数m, n,分别表示矩阵的行数和列数。
接下来的m行,每行n个整数,之间用一个空格分隔,表示矩阵A的元素。
输出
每组数据输出一行,表示选出的数的和的最小值。
数据范围
小数据:1 <= m, n <= 5大数据:1 <= m, n <= 100
样例输入
2 3 3 1 2 3 3 1 2 2 3 1 5 5 1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
样例输出
Case 1: 3 Case 2: 5
代码如下:
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int last;//剩下未覆盖的边或行数
int boolx[100];//经过边或行的数量
int booly[100];
int m,n;//行数,列数
unsigned int mini;
typedef struct point{
unsigned int v;//值
unsigned int x;//坐标
unsigned int y;
};
bool more(const point &a,const point &b){
return a.v>b.v;
}
bool lesss(const point &a,const point &b){
return a.v<b.v;
}
vector<point> all;
vector<point> np;
void delp(int x,int y,int v){//
if(boolx[x]>1&&booly[y]>1){
boolx[x]--;
booly[y]--;
}else mini += v;
}
void addp(int x,int y){
if(boolx[x] == 0)last--;
if(booly[y] == 0)last--;
boolx[x]++;
booly[y]++;
}
int main(){
int i,j,T,t = 1;
unsigned int s;
cin>>T;
point p;
while(T--){
cin>>m>>n;
for( i = 0 ;i<m;i++){
for( j = 0;j<n;j++){
cin>>s;
p.x = i;
p.y = j;
p.v = s;
all.push_back(p);
boolx[i] = 0;
booly[j] = 0;
}
}
sort(all.begin(),all.end(),lesss);
last = n + m;
i = 0;
while(last > 0){
addp(all[i].x,all[i].y);
np.push_back(all[i++]);
}
mini = 0;
sort(np.begin(),np.end(),more);
for( i =0; i<np.size();i++)
delp(np[i].x,np[i].y,np[i].v);
all.clear();
np.clear();
printf("Case %d: ",t++);
cout<<mini<<endl;
}
return 0;
} #include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int last;//剩下未覆盖的边或行数
int boolx[100];//经过边或行的数量
int booly[100];
int m,n;//行数,列数
unsigned int mini;
typedef struct point{
unsigned int v;//值
unsigned int x;//坐标
unsigned int y;
};
bool more(const point &a,const point &b){
return a.v>b.v;
}
bool lesss(const point &a,const point &b){
return a.v<b.v;
}
vector<point> all;
vector<point> np;
void delp(int x,int y,int v){//
if(boolx[x]>1&&booly[y]>1){
boolx[x]--;
booly[y]--;
}else mini += v;
}
void addp(int x,int y){
if(boolx[x] == 0)last--;
if(booly[y] == 0)last--;
boolx[x]++;
booly[y]++;
}
int main(){
int i,j,T,t = 1;
unsigned int s;
cin>>T;
point p;
while(T--){
cin>>m>>n;
for( i = 0 ;i<m;i++){
for( j = 0;j<n;j++){
cin>>s;
p.x = i;
p.y = j;
p.v = s;
all.push_back(p);
boolx[i] = 0;
booly[j] = 0;
}
}
sort(all.begin(),all.end(),lesss);
last = n + m;
i = 0;
while(last > 0){
addp(all[i].x,all[i].y);
np.push_back(all[i++]);
}
mini = 0;
sort(np.begin(),np.end(),more);
for( i =0; i<np.size();i++)
delp(np[i].x,np[i].y,np[i].v);
all.clear();
np.clear();
printf("Case %d: ",t++);
cout<<mini<<endl;
}
return 0;
}
相关文章推荐
- 编程之美2014 资格赛题目3 : 格格取数
- 编程之美2014资格赛 hihoCoder 题目3:格格取数
- 编程之美2014 资格赛题目2 : 大神与三位小伙伴
- 2014编程之美资格赛题目2 : 大神与三位小伙伴(2014-04-12)
- 编程之美2014-资格赛-题目2:大神与三位小伙伴
- 2014编程之美资格赛第三题-格格取数
- (2014编程之美资格赛)题目2 : 大神与三位小伙伴
- 【编程之美资格赛】题目3 : 格格取数
- 2014 编程之美复赛 题目1 : 组队 (错排)
- 编程之美2014资格赛——大神与三个小伙伴
- 2015编程之美资格赛题目3 : 基站选址
- 编程之美2014资格赛 大神与三个小伙伴 小数据
- (1)2014微软编程之美资格赛赛题
- 编程之美2014 资格赛题目1 : 同构
- 编程之美资格赛题目2 : 大神与三位小伙伴
- 编程之美2015 资格赛 hihocoder 题目2: 回文字符序列
- 2014编程之美资格赛之2 大神与三位小伙伴
- 编程之美2015资格赛--题目1 : 2月29日
- 编程之美2014格格取数
- 编程之美2014初赛第二场 题目1 : 神奇的数列