P2053-[SCOI2007]修车
1 #include<bits/stdc++.h> 2 #define _for(i,a,b) for(register int i = (a);i < b;i ++) 3 #define _rep(i,a,b) for(register int i = (a);i > b;i --) 4 #define INF 0x3f3f3f3f 5 #define MOD 100000000 6 #define maxn 100003 7 #define pb push_back 8 #define debug() printf("Miku Check OK!\n") 9 typedef long long ll; 10 11 using namespace std; 12 typedef pair<int,int> P; 13 inline ll read() 14 { 15 ll ans = 0; 16 char ch = getchar(), last = ' '; 17 while(!isdigit(ch)) last = ch, ch = getchar(); 18 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 19 if(last == '-') ans = -ans; 20 return ans; 21 } 22 inline void write(ll x) 23 { 24 if(x < 0) x = -x, putchar('-'); 25 if(x >= 10) write(x / 10); 26 putchar(x % 10 + '0'); 27 } 28 int ver[maxn],Next[maxn],head[maxn],val[maxn],cost[maxn]; 29 //incf[i]为i在此趟BFS流过的流量 30 int vis[maxn],incf[maxn],pre[maxn]; 31 int n,m,s,t,tot,maxflow,ans; 32 int d[maxn]; 33 void add(int x,int y,int w,int c) 34 { 35 ver[++tot] = y,Next[tot] = head[x],head[x] = tot,val[tot] = w,cost[tot] = c; 36 ver[++tot] = x,Next[tot] = head[y],head[y] = tot,val[tot] = 0,cost[tot] = -c; 37 } 38 bool spfa() 39 { 40 memset(vis,0,sizeof(vis)); 41 memset(d,0x3f,sizeof(d)); 42 queue<int> q; 43 q.push(s); 44 vis[s] = 1; 45 d[s] = 0; 46 incf[s] = INF; 47 while(!q.empty()) 48 { 49 int x = q.front(); 50 q.pop(); 51 vis[x] = 0; 52 for(int i = head[x]; i; i = Next[i]) 53 if(val[i]) 54 { 55 int y = ver[i]; 56 if(d[y] > d[x] + cost[i]) 57 { 58 d[y] = d[x] + cost[i]; 59 incf[y] = min(incf[x],val[i]); 60 pre[y] = i; 61 if(!vis[y]) 62 vis[y] = 1,q.push(y); 63 } 64 } 65 } 66 if(d[t]==INF) 67 return false; 68 return true; 69 } 70 void update() 71 { 72 int x = t; 73 while(x != s) 74 { 75 int i = pre[x]; 76 val[i] -= incf[t]; 77 val[i^1] += incf[t]; 78 x = ver[i^1]; 79 } 80 maxflow += incf[t]; 81 ans += d[t] * incf[t]; 82 } 83 int main() 84 { 85 m = read(); 86 n = read(); 87 s = 0; 88 t = 3939; 89 tot = 1; 90 maxflow = 0; 91 _for(i,1,n+1) 92 add(s,i,1,0); 93 _for(i,n+1,n*(m+1)+1) 94 add(i,t,1,0); 95 _for(i,1,n+1) 96 { 97 _for(j,1,m+1) 98 { 99 int c = read(); 100 _for(k,1,n+1) 101 add(i,j*n+k,1,k*c); 102 } 103 } 104 while(spfa()) update(); 105 printf("%.2f",double(double(ans)/double(n))); 106 return 0; 107 }