Certainly! Below is a C++ implementation that solves the problem as described. The approach involves parsing the input to separate the integer and fractional parts of each cell, checking the necessary conditions for possible assignments, and using a max-flow algorithm (Dinic's algorithm) to determine if a valid assignment exists. If a valid assignment is found, it outputs the assignment matrix accordingly.
#include <bits/stdc++.h>
using namespace std;
// Dinic's Algorithm Implementation
struct Edge {
int to, rev;
int cap;
};
class MaxFlow {
public:
int N;
vector<vector<Edge>> graph;
vector<int> level;
vector<int> ptr;
MaxFlow(int N_) : N(N_), graph(N, vector<Edge>()), level(N, -1), ptr(N, 0) {}
void add_edge(int from, int to, int cap){
Edge a = {to, (int)graph[to].size(), cap};
Edge b = {from, (int)(graph[from].size()), 0};
graph[from].push_back(a);
graph[to].push_back(b);
}
bool bfs(int s, int t){
fill(level.begin(), level.end(), -1);
queue<int> q;
q.push(s);
level[s]=0;
while(!q.empty()){
int v = q.front(); q.pop();
for(auto &e : graph[v]){
if(e.cap >0 && level[e.to]==-1){
level[e.to]=level[v]+1;
q.push(e.to);
}
}
}
return level[t]!=-1;
}
int dfs(int v, int t, int pushed){
if(v == t) return pushed;
for(int &cid = ptr[v]; cid < graph[v].size(); cid++){
Edge &e = graph[v][cid];
if(e.cap >0 && level[e.to]==level[v]+1){
int tr = dfs(e.to, t, min(pushed, e.cap));
if(tr >0){
graph[v][cid].cap -= tr;
graph[e.to][e.rev].cap += tr;
return tr;
}
}
}
return 0;
}
int max_flow(int s, int t){
int flow=0;
while(bfs(s, t)){
fill(ptr.begin(), ptr.end(), 0);
while(int pushed = dfs(s, t, INT32_MAX)) {
flow += pushed;
}
}
return flow;
}
};
// Function to parse x_i,j from string to (floor, frac_scaled)
pair<long long, long long> parse_x(const string& s){
size_t pos = s.find('.');
if(pos == string::npos){
// No decimal point
long long floor_val = stoll(s);
return {floor_val, 0};
}
string int_part = s.substr(0, pos);
string frac_part = s.substr(pos+1);
// Ensure frac_part has exactly 3 digits
while(frac_part.size() <3) frac_part += '0';
if(frac_part.size() >3){
frac_part = frac_part.substr(0,3);
}
long long floor_val = 0;
if(int_part.empty() || int_part == "-"){
// Handle cases like ".500" or "-.500"
if(int_part == "-"){
floor_val = 0; // Temporary, will adjust below
}
else{
floor_val = 0;
}
}
else{
floor_val = stoll(int_part);
}
long long frac_scaled = stoll(frac_part);
// Handle negative numbers
if(s[0] == '-' && floor_val > 0){
floor_val -=1;
frac_scaled = 1000 - frac_scaled;
}
return {floor_val, frac_scaled};
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
int n, m;
cin >> n >> m;
// Read the grid
vector<vector<string>> grid(n, vector<string>(m));
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
cin >> grid[i][j];
}
}
// Parse the grid
vector<vector<long long>> floor_grid(n, vector<long long>(m, 0));
vector<vector<long long>> frac_grid(n, vector<long long>(m, 0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
pair<long long, long long> p = parse_x(grid[i][j]);
floor_grid[i][j] = p.first;
frac_grid[i][j] = p.second;
}
}
// Compute row needs and column needs
vector<long long> row_frac_scaled(n, 0);
vector<long long> col_frac_scaled(m, 0);
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
row_frac_scaled[i] += frac_grid[i][j];
col_frac_scaled[j] += frac_grid[i][j];
}
}
bool possible=true;
vector<long long> row_need(n,0);
vector<long long> col_need(m,0);
for(int i=0;i<n;i++) {
if(row_frac_scaled[i] % 1000 !=0){
possible=false;
break;
}
row_need[i] = row_frac_scaled[i]/1000;
}
if(possible){
for(int j=0;j<m;j++) {
if(col_frac_scaled[j] % 1000 !=0){
possible=false;
break;
}
col_need[j] = col_frac_scaled[j]/1000;
}
}
if(possible){
long long total_row_need=0, total_col_need=0;
for(int i=0;i<n;i++) total_row_need += row_need[i];
for(int j=0;j<m;j++) total_col_need += col_need[j];
if(total_row_need != total_col_need){
possible=false;
}
}
if(!possible){
cout << "NO\n";
continue;
}
// Build the flow network
// Nodes: 0 - source, 1..n - rows, n+1..n+m - columns, n+m+1 - sink
int S = 0;
int T_node = n + m +1;
int total_nodes = n + m +2;
MaxFlow mf(total_nodes);
for(int i=0;i<n;i++){
mf.add_edge(S, 1+i, row_need[i]);
}
for(int j=0;j<m;j++){
mf.add_edge(1+n +j, T_node, col_need[j]);
}
// Add edges between rows and columns where frac >0
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(frac_grid[i][j] >0){
mf.add_edge(1+i, 1+n +j, 1);
}
}
}
// Compute max flow
int flow = mf.max_flow(S, T_node);
long long total_need = 0;
for(int i=0;i<n;i++) total_need += row_need[i];
if(flow != total_need){
cout << "NO\n";
continue;
}
// Reconstruct the assignments
vector<vector<int>> y(n, vector<int>(m, 0));
for(int i=0;i<n;i++) {
for(auto &e : mf.graph[1+i]){
if(e.to >= 1+n && e.to < 1+n+m && e.cap ==0){
int j = e.to - (1+n);
y[i][j] =1;
}
}
}
// Assign y=0 for frac=0 cells
// Output the result
cout << "YES\n";
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(j >0) cout << ' ';
cout << y[i][j];
}
cout << '\n';
}
}
}