QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423535#2208. FlowKinNaAC ✓105ms4212kbC++141.7kb2024-05-28 08:28:572024-05-28 08:28:57

Judging History

你现在查看的是最新测评结果

  • [2024-05-28 08:28:57]
  • 评测
  • 测评结果:AC
  • 用时:105ms
  • 内存:4212kb
  • [2024-05-28 08:28:57]
  • 提交

answer

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 2010;

struct Edge {int y, nxt; LL w, c;} e[N << 4]; int ly[N], ltp = 1;
void add(int x, int y, LL w, LL c) {e[++ltp] = {y, ly[x], w, c}; ly[x] = ltp;}

LL dis[N]; int cur[N]; bool inq[N];
bool spfa(int s, int t) {
	memset(dis, 0x3f, sizeof dis);
	memset(inq, 0, sizeof inq);
	queue <int> q; q.push(s); dis[s] = 0;
	while (!q.empty()) {
		int x = q.front(); q.pop(); inq[x] = 0; cur[x] = ly[x];
		for (int i = ly[x], y; y = e[i].y, i; i = e[i].nxt) {
			if (e[i].w && dis[y] > dis[x] + e[i].c) {
				dis[y] = dis[x] + e[i].c;
				if (!inq[y]) q.push(y), inq[y] = 1;
			}
		}
	}
	return dis[t] != 0x3f3f3f3f3f3f3f3f;
}
LL sum, mxf, mnc;
LL dfs(int x, int t, LL In = 0x3f3f3f3f3f3f3f3f) {
	if (x == t) return In; LL Out = 0;
	inq[x] = 1;
	for (int i = cur[x], y; y = e[i].y, i && In; i = e[i].nxt) {
		cur[x] = i;
		if (!inq[y] && e[i].w && dis[y] == dis[x] + e[i].c) {
			LL res = dfs(y, t, min(In, e[i].w));
			e[i].w -= res; e[i ^ 1].w += res;
			In -= res; Out += res;
			mnc += res * e[i].c;
		}
	} 
	inq[x] = 0;
	if (!Out) dis[x] = 0x3f3f3f3f3f3f3f3f;
	return Out;
}

int n, m, ss, tt;
LL delta[N];

int main() {
	cin >> n >> m; ss = n + 1; tt = n + 2;
	for (int i = 1, x, y, w; i <= m; ++i) {
		cin >> x >> y >> w;
		delta[x] -= w; delta[y] += w; mnc -= w;
		add(x, y, 0, -1); add(y, x, w, 1);
	}
	for (int i = 1; i <= n; ++i) {
		if (delta[i] > 0) add(ss, i, delta[i], 0), add(i, ss, 0, 0), sum += delta[i];
		else if (delta[i] < 0) add(i, tt, -delta[i], 0), add(tt, i, 0, 0);
	}
	while (spfa(ss, tt))
		mxf += dfs(ss, tt);
	cout << -mnc << endl;
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 50ms
memory: 4140kb

Test #2:

score: 0
Accepted
time: 16ms
memory: 3736kb

Test #3:

score: 0
Accepted
time: 27ms
memory: 3940kb

Test #4:

score: 0
Accepted
time: 41ms
memory: 4112kb

Test #5:

score: 0
Accepted
time: 53ms
memory: 4120kb

Test #6:

score: 0
Accepted
time: 61ms
memory: 3856kb

Test #7:

score: 0
Accepted
time: 73ms
memory: 3960kb

Test #8:

score: 0
Accepted
time: 29ms
memory: 3840kb

Test #9:

score: 0
Accepted
time: 38ms
memory: 3932kb

Test #10:

score: 0
Accepted
time: 46ms
memory: 3944kb

Test #11:

score: 0
Accepted
time: 0ms
memory: 3848kb

Test #12:

score: 0
Accepted
time: 48ms
memory: 3888kb

Test #13:

score: 0
Accepted
time: 68ms
memory: 4172kb

Test #14:

score: 0
Accepted
time: 84ms
memory: 3988kb

Test #15:

score: 0
Accepted
time: 9ms
memory: 4100kb

Test #16:

score: 0
Accepted
time: 28ms
memory: 3904kb

Test #17:

score: 0
Accepted
time: 33ms
memory: 3900kb

Test #18:

score: 0
Accepted
time: 7ms
memory: 3816kb

Test #19:

score: 0
Accepted
time: 60ms
memory: 3980kb

Test #20:

score: 0
Accepted
time: 2ms
memory: 3840kb

Test #21:

score: 0
Accepted
time: 48ms
memory: 4008kb

Test #22:

score: 0
Accepted
time: 32ms
memory: 4116kb

Test #23:

score: 0
Accepted
time: 75ms
memory: 3952kb

Test #24:

score: 0
Accepted
time: 61ms
memory: 4148kb

Test #25:

score: 0
Accepted
time: 59ms
memory: 3984kb

Test #26:

score: 0
Accepted
time: 41ms
memory: 3984kb

Test #27:

score: 0
Accepted
time: 11ms
memory: 3844kb

Test #28:

score: 0
Accepted
time: 17ms
memory: 4112kb

Test #29:

score: 0
Accepted
time: 68ms
memory: 4124kb

Test #30:

score: 0
Accepted
time: 85ms
memory: 4200kb

Test #31:

score: 0
Accepted
time: 32ms
memory: 3916kb

Test #32:

score: 0
Accepted
time: 100ms
memory: 3912kb

Test #33:

score: 0
Accepted
time: 49ms
memory: 3952kb

Test #34:

score: 0
Accepted
time: 68ms
memory: 3976kb

Test #35:

score: 0
Accepted
time: 25ms
memory: 3928kb

Test #36:

score: 0
Accepted
time: 49ms
memory: 3980kb

Test #37:

score: 0
Accepted
time: 57ms
memory: 3912kb

Test #38:

score: 0
Accepted
time: 65ms
memory: 3980kb

Test #39:

score: 0
Accepted
time: 40ms
memory: 4176kb

Test #40:

score: 0
Accepted
time: 48ms
memory: 3928kb

Test #41:

score: 0
Accepted
time: 12ms
memory: 3868kb

Test #42:

score: 0
Accepted
time: 22ms
memory: 3896kb

Test #43:

score: 0
Accepted
time: 32ms
memory: 4148kb

Test #44:

score: 0
Accepted
time: 51ms
memory: 3908kb

Test #45:

score: 0
Accepted
time: 55ms
memory: 3920kb

Test #46:

score: 0
Accepted
time: 70ms
memory: 4120kb

Test #47:

score: 0
Accepted
time: 43ms
memory: 3868kb

Test #48:

score: 0
Accepted
time: 63ms
memory: 3964kb

Test #49:

score: 0
Accepted
time: 63ms
memory: 4164kb

Test #50:

score: 0
Accepted
time: 44ms
memory: 3996kb

Test #51:

score: 0
Accepted
time: 19ms
memory: 3852kb

Test #52:

score: 0
Accepted
time: 10ms
memory: 3896kb

Test #53:

score: 0
Accepted
time: 5ms
memory: 3852kb

Test #54:

score: 0
Accepted
time: 64ms
memory: 3980kb

Test #55:

score: 0
Accepted
time: 3ms
memory: 3852kb

Test #56:

score: 0
Accepted
time: 69ms
memory: 4012kb

Test #57:

score: 0
Accepted
time: 16ms
memory: 3840kb

Test #58:

score: 0
Accepted
time: 58ms
memory: 3908kb

Test #59:

score: 0
Accepted
time: 71ms
memory: 4180kb

Test #60:

score: 0
Accepted
time: 46ms
memory: 3980kb

Test #61:

score: 0
Accepted
time: 69ms
memory: 4016kb

Test #62:

score: 0
Accepted
time: 55ms
memory: 3912kb

Test #63:

score: 0
Accepted
time: 44ms
memory: 3908kb

Test #64:

score: 0
Accepted
time: 8ms
memory: 3792kb

Test #65:

score: 0
Accepted
time: 45ms
memory: 3948kb

Test #66:

score: 0
Accepted
time: 64ms
memory: 3968kb

Test #67:

score: 0
Accepted
time: 7ms
memory: 3868kb

Test #68:

score: 0
Accepted
time: 49ms
memory: 3964kb

Test #69:

score: 0
Accepted
time: 32ms
memory: 3928kb

Test #70:

score: 0
Accepted
time: 29ms
memory: 3908kb

Test #71:

score: 0
Accepted
time: 2ms
memory: 3812kb

Test #72:

score: 0
Accepted
time: 14ms
memory: 4088kb

Test #73:

score: 0
Accepted
time: 39ms
memory: 4120kb

Test #74:

score: 0
Accepted
time: 31ms
memory: 4180kb

Test #75:

score: 0
Accepted
time: 31ms
memory: 3900kb

Test #76:

score: 0
Accepted
time: 30ms
memory: 4132kb

Test #77:

score: 0
Accepted
time: 26ms
memory: 3944kb

Test #78:

score: 0
Accepted
time: 53ms
memory: 3884kb

Test #79:

score: 0
Accepted
time: 66ms
memory: 4208kb

Test #80:

score: 0
Accepted
time: 43ms
memory: 3896kb

Test #81:

score: 0
Accepted
time: 28ms
memory: 3812kb

Test #82:

score: 0
Accepted
time: 62ms
memory: 3964kb

Test #83:

score: 0
Accepted
time: 45ms
memory: 3960kb

Test #84:

score: 0
Accepted
time: 46ms
memory: 3924kb

Test #85:

score: 0
Accepted
time: 47ms
memory: 3916kb

Test #86:

score: 0
Accepted
time: 45ms
memory: 3896kb

Test #87:

score: 0
Accepted
time: 0ms
memory: 3728kb

Test #88:

score: 0
Accepted
time: 45ms
memory: 3924kb

Test #89:

score: 0
Accepted
time: 18ms
memory: 3832kb

Test #90:

score: 0
Accepted
time: 77ms
memory: 4176kb

Test #91:

score: 0
Accepted
time: 30ms
memory: 3968kb

Test #92:

score: 0
Accepted
time: 0ms
memory: 4044kb

Test #93:

score: 0
Accepted
time: 28ms
memory: 4136kb

Test #94:

score: 0
Accepted
time: 76ms
memory: 3956kb

Test #95:

score: 0
Accepted
time: 27ms
memory: 3952kb

Test #96:

score: 0
Accepted
time: 9ms
memory: 4024kb

Test #97:

score: 0
Accepted
time: 41ms
memory: 3816kb

Test #98:

score: 0
Accepted
time: 77ms
memory: 3976kb

Test #99:

score: 0
Accepted
time: 3ms
memory: 3800kb

Test #100:

score: 0
Accepted
time: 55ms
memory: 3912kb

Test #101:

score: 0
Accepted
time: 92ms
memory: 3908kb

Test #102:

score: 0
Accepted
time: 68ms
memory: 3976kb

Test #103:

score: 0
Accepted
time: 14ms
memory: 3808kb

Test #104:

score: 0
Accepted
time: 83ms
memory: 3980kb

Test #105:

score: 0
Accepted
time: 48ms
memory: 3940kb

Test #106:

score: 0
Accepted
time: 78ms
memory: 3956kb

Test #107:

score: 0
Accepted
time: 13ms
memory: 3824kb

Test #108:

score: 0
Accepted
time: 41ms
memory: 3952kb

Test #109:

score: 0
Accepted
time: 25ms
memory: 3832kb

Test #110:

score: 0
Accepted
time: 48ms
memory: 3976kb

Test #111:

score: 0
Accepted
time: 43ms
memory: 4148kb

Test #112:

score: 0
Accepted
time: 27ms
memory: 4116kb

Test #113:

score: 0
Accepted
time: 85ms
memory: 3888kb

Test #114:

score: 0
Accepted
time: 49ms
memory: 4200kb

Test #115:

score: 0
Accepted
time: 82ms
memory: 3964kb

Test #116:

score: 0
Accepted
time: 6ms
memory: 4028kb

Test #117:

score: 0
Accepted
time: 35ms
memory: 3924kb

Test #118:

score: 0
Accepted
time: 16ms
memory: 3864kb

Test #119:

score: 0
Accepted
time: 34ms
memory: 3932kb

Test #120:

score: 0
Accepted
time: 47ms
memory: 3948kb

Test #121:

score: 0
Accepted
time: 54ms
memory: 3972kb

Test #122:

score: 0
Accepted
time: 59ms
memory: 3932kb

Test #123:

score: 0
Accepted
time: 37ms
memory: 4136kb

Test #124:

score: 0
Accepted
time: 49ms
memory: 4212kb

Test #125:

score: 0
Accepted
time: 74ms
memory: 3948kb

Test #126:

score: 0
Accepted
time: 42ms
memory: 3944kb

Test #127:

score: 0
Accepted
time: 2ms
memory: 3836kb

Test #128:

score: 0
Accepted
time: 57ms
memory: 4004kb

Test #129:

score: 0
Accepted
time: 80ms
memory: 3952kb

Test #130:

score: 0
Accepted
time: 4ms
memory: 3796kb

Test #131:

score: 0
Accepted
time: 57ms
memory: 3876kb

Test #132:

score: 0
Accepted
time: 14ms
memory: 3824kb

Test #133:

score: 0
Accepted
time: 42ms
memory: 3864kb

Test #134:

score: 0
Accepted
time: 43ms
memory: 3988kb

Test #135:

score: 0
Accepted
time: 67ms
memory: 3968kb

Test #136:

score: 0
Accepted
time: 47ms
memory: 3920kb

Test #137:

score: 0
Accepted
time: 26ms
memory: 3944kb

Test #138:

score: 0
Accepted
time: 36ms
memory: 4156kb

Test #139:

score: 0
Accepted
time: 30ms
memory: 3972kb

Test #140:

score: 0
Accepted
time: 81ms
memory: 3964kb

Test #141:

score: 0
Accepted
time: 55ms
memory: 3884kb

Test #142:

score: 0
Accepted
time: 76ms
memory: 4196kb

Test #143:

score: 0
Accepted
time: 65ms
memory: 3964kb

Test #144:

score: 0
Accepted
time: 58ms
memory: 3972kb

Test #145:

score: 0
Accepted
time: 47ms
memory: 3856kb

Test #146:

score: 0
Accepted
time: 39ms
memory: 3864kb

Test #147:

score: 0
Accepted
time: 51ms
memory: 3924kb

Test #148:

score: 0
Accepted
time: 68ms
memory: 3884kb

Test #149:

score: 0
Accepted
time: 54ms
memory: 3972kb

Test #150:

score: 0
Accepted
time: 12ms
memory: 3872kb

Test #151:

score: 0
Accepted
time: 40ms
memory: 3940kb

Test #152:

score: 0
Accepted
time: 58ms
memory: 3916kb

Test #153:

score: 0
Accepted
time: 63ms
memory: 3972kb

Test #154:

score: 0
Accepted
time: 26ms
memory: 3864kb

Test #155:

score: 0
Accepted
time: 70ms
memory: 3948kb

Test #156:

score: 0
Accepted
time: 64ms
memory: 4212kb

Test #157:

score: 0
Accepted
time: 13ms
memory: 4116kb

Test #158:

score: 0
Accepted
time: 80ms
memory: 3960kb

Test #159:

score: 0
Accepted
time: 46ms
memory: 4136kb

Test #160:

score: 0
Accepted
time: 20ms
memory: 3924kb

Test #161:

score: 0
Accepted
time: 29ms
memory: 3896kb

Test #162:

score: 0
Accepted
time: 25ms
memory: 3880kb

Test #163:

score: 0
Accepted
time: 16ms
memory: 3888kb

Test #164:

score: 0
Accepted
time: 69ms
memory: 3828kb

Test #165:

score: 0
Accepted
time: 7ms
memory: 3832kb

Test #166:

score: 0
Accepted
time: 44ms
memory: 3952kb

Test #167:

score: 0
Accepted
time: 39ms
memory: 4196kb

Test #168:

score: 0
Accepted
time: 73ms
memory: 3992kb

Test #169:

score: 0
Accepted
time: 105ms
memory: 4180kb

Test #170:

score: 0
Accepted
time: 56ms
memory: 3960kb

Test #171:

score: 0
Accepted
time: 87ms
memory: 3948kb

Test #172:

score: 0
Accepted
time: 24ms
memory: 3896kb

Test #173:

score: 0
Accepted
time: 57ms
memory: 3976kb

Test #174:

score: 0
Accepted
time: 63ms
memory: 4120kb

Test #175:

score: 0
Accepted
time: 40ms
memory: 4140kb

Test #176:

score: 0
Accepted
time: 73ms
memory: 4140kb

Test #177:

score: 0
Accepted
time: 23ms
memory: 3896kb

Test #178:

score: 0
Accepted
time: 55ms
memory: 3912kb

Test #179:

score: 0
Accepted
time: 44ms
memory: 3880kb

Test #180:

score: 0
Accepted
time: 7ms
memory: 3856kb

Test #181:

score: 0
Accepted
time: 53ms
memory: 3912kb

Test #182:

score: 0
Accepted
time: 24ms
memory: 4152kb

Test #183:

score: 0
Accepted
time: 46ms
memory: 3916kb

Test #184:

score: 0
Accepted
time: 65ms
memory: 3956kb

Test #185:

score: 0
Accepted
time: 64ms
memory: 3924kb

Test #186:

score: 0
Accepted
time: 61ms
memory: 4020kb