编程之美2014资格赛题目3:格格取数
来源:互联网 发布:淘宝上苹果找回可信吗 编辑:程序博客网 时间:2024/04/27 23:00
编程之美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; }
0 0
- 2014编程之美资格赛 题目3 : 格格取数
- 编程之美2014资格赛题目3:格格取数
- 【编程之美资格赛】题目3 : 格格取数
- 编程之美2014资格赛 hihoCoder 题目3:格格取数
- 2014编程之美资格赛第三题-格格取数
- 编程之美2014格格取数
- 编程之美2014格格取数
- 编程之美2014格格取数
- 2014 编程之美 资格赛 第三题 格格取数(二分图带权最小边覆盖)
- 编程之美之格格取数
- 编程之美 格格取数问题
- 编程之美-格格取数
- 编程之美2014格格取数(回溯法)
- [2014]编程之美第3题,格格取数问题
- 编程之美 打酱油 格格取数
- 编程之美 格格取数 C语言版本
- 编程之美 2014 格格取数(最小费用最大流)
- 2014 编程之美 预赛第三题 格格取数 上下界费用流
- 水利部:水质考核不合格限期一个月内出整改方案
- 杭电 1052 Tian Ji -- The Horse Racing
- POJ 1664 放苹果
- 遗落的过往
- OpenCV学习笔记(二)
- 编程之美2014资格赛题目3:格格取数
- Java字符编码根本原理
- 日媒:疑似中国潜艇在冲绳近海潜行 未进日领海
- Qt下使用OpenGL(20)-根据NeHe的教程改写的
- SSL Socket 双向认证
- 我国新型导弹驱逐舰昆明舰交付海军
- POJ1182食物链
- 杭电acm 2616
- hibernate Annotation一对一双向外键关联