QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#136168 | #5042. Flow | PetroTarnavskyi# | RE | 0ms | 0kb | C++17 | 1.6kb | 2023-08-07 14:58:42 | 2023-08-07 14:58:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int N = 1 << 17;
vector<PII> g[N];
VI vals[N];
int n;
void dfs(int v, int p, int t){
if(v == n - 1)
return;
for(auto e : g[v]){
int to = e.F;
if(p == to)
continue;
vals[t].PB(e.S);
dfs(to, v,t);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> n >> m;
LL sum = 0;
FOR(i, 0, m){
int a, b, c;
cin >> a >> b >> c;
a--; b--;
g[a].PB(MP(b, c));
g[b].PB(MP(a, c));
sum += c;
}
int sz = SZ(g[0]);
FOR(i, 0, sz){
vals[i].PB(g[0][i].S);
dfs(g[0][i].F, 0, i);
}
assert(m - (n - 2) == SZ(g[0]));
assert(m % (m - (n - 2)) == 0);
int edges = m / (m - (n - 2));
LL want = sum / edges;
LL cur = 0;
set<PII> cnts;
FOR(i, 0, sz){
sort(ALL(vals[i]));
cur += vals[i][0];
cnts.insert(MP(1, i));
}
cout << want << " " << cur << endl;
LL ans = 0;
while(want != cur){
assert(SZ(cnts));
auto best = *cnts.begin();
cnts.erase(cnts.begin());
assert(best.F != SZ(vals[0]));
LL change = min(want - cur, (LL)vals[best.S][best.F] - vals[best.S][best.F - 1]);
ans += change * best.F;
best.F++;
cnts.insert(best);
}
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
4 3 1 2 1 2 3 2 3 4 3
output:
2 1