Pagini recente » Cod sursa (job #3299540) | Cod sursa (job #1708059) | Cod sursa (job #2643075) | Cod sursa (job #2468917) | Cod sursa (job #563090)
Cod sursa(job #563090)
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("algebra.in");
ofstream fout("algebra.out");
const int maxn = 35;
const double eps = 0.000001;
bool seen[maxn];
int a;
int i , j , k , n , L[maxn] , R[maxn] , sol[maxn][maxn * maxn] , v[maxn] , ans , cnt;
long double cost[maxn][maxn];
double d_abs( double a ) {
if ( a < 0 )
return -a;
return a;
}
bool match (int node)
{
if ( seen[node] ) return false;
seen[node] = true;
int i;
for( i = 1 ; i <= n ; ++i )
if ( cost[node][i] )
if ( L[i] == 0 || match ( L[i] ) ) {
R[node] = i;
L[i] = node;
return true;
}
return false;
}
void matching()
{
int i;
bool ok = true;
memset(L , 0 , sizeof(L));
memset(R, 0 , sizeof(R));
for( ; ok ; ) {
ok = false;
memset(seen , 0 , sizeof(seen));
for( i = 1 ; i <= n ; ++i )
if ( R[i] == 0 )
if ( match(i) )
ok = true;
}
}
int main()
{
fin >> n;
for( i = 1 ; i <= n ; ++i )
for( j = 1 ; j <= n ; ++j )
fin >> cost[i][j];
for(cnt = n * n ; cnt ; ) {
ans++;
long double mins = 1;
matching();
for( i = 1 ; i <= n ; ++i )
if ( cost[i][R[i]] < mins )
mins = cost[i][R[i]];
v[ans] = mins;
for( i = 1 ; i <= n ; ++i ) {
sol[ans][i] = R[i];
cost[i][ R[i] ] -= mins;
if ( cost[i][ R[i] ] == 0)
cnt--;
}
for ( i = 1 ; i <= n ; ++i ) {
for( j = 1 ; j <= n ; ++j )
fout << cost[i][j] <<" ";
fout <<"\n";
}
fout<<"\n";
}
fout << ans << "\n";
for( i = 1 ; i <= ans ; ++i ) {
fout << v[i] << "\n";
for( j = 1 ; j <= n ; ++k )
fout << sol[i][j] <<" ";
fout <<"\n";
}
return 0;
}