QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#131680 | #853. Flat Organization | _LAP_ | WA | 15ms | 7936kb | C++14 | 3.6kb | 2023-07-27 20:42:45 | 2023-07-27 20:42:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2005; const long long INF = 0x3f3f3f3f3f3f3f3fLL;
int n, d[N][N];
pair<int, pair<int, int>> G[N][N]; int c[N][N];
int pre[N], low[N], dfs_clock;
stack<int> stk; int sccno[N], scc_cnt;
int pr[N], ind[N]; long long f[N], S[N];
inline void init() {
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
c[i][j] = 0, G[i][j] = {0, {0, 0}};
for(int i = 0; i < n; i ++)
ind[i] = pre[i] = low[i] = sccno[i] = pr[i] = f[i] = S[i] = 0;
dfs_clock = scc_cnt = 0;
}
void Tarjan(int u) {
pre[u] = low[u] = ++dfs_clock; stk.push(u);
for(int v = 0; v < n; v ++) if(d[u][v]) {
if(!pre[v]) {
Tarjan(v); low[u] = min(low[u], low[v]);
} else if(!sccno[v]) low[u] = min(low[u], pre[v]);
}
if(low[u] >= pre[u]) {
int y; ++scc_cnt;
do {
y = stk.top(); stk.pop(); sccno[y] = scc_cnt;
} while(y != u);
}
}
inline vector<int> Topo() {
queue<int> Q; vector<int> ans;
for(int i = 1; i <= scc_cnt; i ++)
if(!ind[i]) Q.push(i);
while(!Q.empty()) {
int u = Q.front(); Q.pop(); ans.emplace_back(u);
for(int v = 1; v <= scc_cnt; v ++) if(c[u][v]) {
if(!--ind[v]) Q.push(v);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T; cin >> T;
while(T --) {
cin >> n;
init();
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
cin >> d[i][j];
for(int i = 0; i < n; i ++)
if(!pre[i]) Tarjan(i);
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++) if(sccno[i] != sccno[j] && d[i][j]) {
int x = sccno[i], y = sccno[j];
c[x][y] ++;
if(c[x][y] == 1) G[x][y] = {d[i][j], {i + 1, j + 1}};
else G[x][y] = min(G[x][y], {d[i][j], {i + 1, j + 1}});
ind[y] += (c[x][y] == 1);
}
vector<int> P = Topo();
for(int i = 1; i < scc_cnt; i ++) S[i] = S[i - 1] + G[P[i - 1]][P[i]].first;
assert(P.size() == scc_cnt);
if(scc_cnt == 1) {cout << "YES\n0 0\n"; continue; }
for(int i = 1; i < scc_cnt; i ++) {
int u = P[i];
f[i] = INF;
if(c[P[i - 1]][P[i]] >= 2) f[i] = min(f[i], f[i - 1] + G[P[i - 1]][P[i]].first);
for(int j = 0; j < i - 1; j ++) {
f[i] = min(f[i], f[j] + G[P[j]][P[i]].first);
f[i] = min(f[i], f[j] + (S[i] - S[j]));
}
}
if(f[scc_cnt - 1] == INF) {cout << "NO\n"; continue; }
cout << "YES\n";
vector<pair<int, int>> ans;
for(int i = scc_cnt - 1; i; ) {
if(c[P[i - 1]][P[i]] >= 2 && f[i - 1] + G[P[i - 1]][P[i]].first == f[i]) {
ans.emplace_back(G[P[i - 1]][P[i]].second);
i = i - 1; continue;
}
for(int j = 0; j < i - 1; j ++) {
if(f[i] == f[j] + G[P[j]][P[i]].first) {
ans.emplace_back(G[P[j]][P[i]].second);
i = j; break;
} else if(f[i] == f[j] + (S[i] - S[j])) {
for(int k = j + 1; k <= i; k ++)
ans.emplace_back(G[P[k - 1]][P[k]].second);
i = j; break;
}
}
}
cout << ans.size() << ' ' << f[scc_cnt - 1] << '\n';
for(auto &pr : ans) cout << pr.first << ' ' << pr.second << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7696kb
input:
1 5 0 1 0 6 11 0 0 1 6 12 2 0 0 7 12 0 0 0 0 4 0 0 0 0 0
output:
YES 2 10 1 4 4 5
result:
ok OK!
Test #2:
score: -100
Wrong Answer
time: 15ms
memory: 7936kb
input:
100 5 0 1 0 6 11 0 0 1 6 12 2 0 0 7 12 0 0 0 0 4 0 0 0 0 0 1 0 2 0 0 7 0 2 0 1000000000 0 0 3 0 0 5 6 0 0 0 7 0 3 0 4 6 0 0 0 0 1 0 3 0 4 0 0 0 0 6 3 0 3 0 0 0 10 0 6 3 0 0 3 0 1999999998 1999999999 0 0 1999999998 0 0 0 50 0 105800 801 1800 64800 0 259200 288801 72201 12801 125000 20001 28801 33800 ...
output:
YES 2 10 1 4 4 5 YES 0 0 NO NO YES 0 0 YES 1 1 3 2 YES 2 3 0 0 3 2 YES 1 3 3 1 YES 2 3 0 0 3 2 YES 3 602 34 39 11 47 4 33 YES 3 649 17 29 32 45 27 12 YES 5 1209 1 18 14 35 35 4 4 25 25 12 YES 3 844 14 21 1 41 3 8 YES 3 866 46 26 35 44 17 35 YES 4 1066 37 8 24 2 36 24 10 28 YES 3 1122 5 22 43 19 4 17...
result:
wrong answer Test 6: The resulting graph is not strongly connected