bzoj 4004 向量拟阵

题解RT.

 

eps = 1e-10 WrongAnswer

eps = 1e-5 Accepted

 

 1 /**************************************************************
 2     Problem: 4004
 3     User: idy002
 4     Language: C++
 5     Result: Accepted
 6     Time:516 ms
 7     Memory:2844 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13 #define eps 1e-5
14 #define N 510
15 using namespace std;
16  
17 int n, m;
18 double aa[N][N]; 
19 int cost[N];
20 int vid[N];
21 int kid[N];
22  
23 int sg( double x ) {
24     return (x>-eps)-(x<eps);
25 }
26 bool cmp( int a, int b ) { return cost[a]<cost[b]; }
27 bool join( int id ) {
28     for( int i=1; i<=m; i++ ) {
29         if( sg(aa[id][i])==0 ) continue;
30         if( kid[i]==0 ) {
31             kid[i]=id;
32             return true;
33         } 
34         int uid = kid[i];
35         double k = aa[id][i]/aa[uid][i];
36         for( int j=i; j<=m; j++ )
37             aa[id][j] -= k*aa[uid][j];
38     }
39     return false;
40 }
41 int main() {
42     scanf( "%d%d", &n, &m );
43     for( int i=1; i<=n; i++ )
44         for( int j=1; j<=m; j++ )
45             scanf( "%lf", &aa[i][j] );
46     for( int i=1; i<=n; i++ )
47         scanf( "%d", cost+i );
48     for( int i=1; i<=n; i++ )
49         vid[i] = i;
50     sort( vid+1, vid+1+n, cmp );
51     memset( kid, 0, sizeof(kid) );
52     int cnt=0, ans=0;
53     for( int i=1; i<=n; i++ ) {
54         int id = vid[i];
55         if( join(id) ) {
56             cnt++;
57             ans+=cost[id];
58         }
59     }
60     printf( "%d %d\n", cnt, ans );
61 }
View Code

 

posted @ 2015-06-09 20:05  idy002  阅读(166)  评论(0编辑  收藏  举报