题解 | #建物流中转站#
建物流中转站
http://www.nowcoder.com/practice/c82efaf9e2cc42cda0a8ad795845eceb
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100010;
int sx[N], sy[N], disx[N], disY[N];
int px[N], py[N], cnt;
int n;
int main() {
cin >> n;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
int x;
cin >> x;
if(x == 0) {
px[cnt] = i, py[cnt] = j, cnt++;
} else {
sx[i] ++ , sy[j] ++;
}
}
}
for(int i=0; i<n; i++) {
int dx = 0, dy = 0;
for(int j=0; j<n; j++) {
dx += abs(j-i)*sx[j], dy += abs(j-i)*sy[j];
}
disx[i] = dx, disY[i] = dy;
}
int res = 0x3f3f3f3f;
for(int i=0; i<cnt; i++) {
int x = px[i], y = py[i];
res = min(res, disx[x] + disY[y]);
}
if(cnt) cout << res << endl;
else cout << -1 << endl;
return 0;
}