Submission #1321709


Source Code Expand

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <functional>
using namespace std;

#define int long long
static const int INF = 1e13;
struct MinCostFlow {
	struct E {
		//to,容量,コスト,逆辺のindex
		int to, cap, cost, rev;
		E(int t, int ca, int co, int r) {
			to = t;
			cap = ca;
			cost = co;
			rev = r;
		}
	};

	//最大頂点数
	int V;
	vector<vector<E>>G;
	MinCostFlow(int v) {
		V = v;
		G.resize(V);
	}

	//有向辺
	inline void add_edge(int from, int to, int cap, int cost) {
		if (from == to)return;
		E e = E(to, cap, cost, G[to].size());
		E ee = E(from, 0, -cost, G[from].size());
		G[from].push_back(e);
		G[to].push_back(ee);
	}

	//コストを距離とみる
	//コスト,最小コスト
	vector<int>dist, mindist;
	//パス復元用
	vector<int>pre_v, pre_i;

	//始点から各頂点までの最小コスト
	void dijkstra(int s) {
		priority_queue< pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > que;
		dist.assign(V, INF);
		dist[s] = 0;
		que.push(pair<int,int>(0,s));
		while (!que.empty()) {
			pair<int,int> p = que.top(); que.pop();
			int v = p.second;
			if (dist[v] < p.first)continue;

			for (int i = 0; i < G[v].size(); i++) {
				E &e = G[v][i];
				if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + mindist[v] - mindist[e.to]) {
					dist[e.to] = dist[v] + e.cost + mindist[v] - mindist[e.to];
					pre_v[e.to] = v;
					pre_i[e.to] = i;
					que.push(pair<int,int>(dist[e.to], e.to));
				}
			}
		}
	}

	//最小費用を返す
	int min_cost_flow(int s, int t, int flow) {
		int cost = 0;
		mindist.resize(V);
		pre_v.resize(V);
		pre_i.resize(V);

		//フローを流しつくすまで
		while (flow > 0) {
			dijkstra(s);
			//もう流せなかったら
			if (dist[t] == INF)return cost;

			//最小コストの更新
			for (int v = 0; v < V; v++)mindist[v] += dist[v];

			//最小コストパス上の最大流
			int f = flow;
			for (int v = t; v != s; v = pre_v[v]) {
				f = min(f, G[pre_v[v]][pre_i[v]].cap);	
			}
			flow -= f;

			//費用の更新
			cost += f * mindist[t];

			//容量の更新
			for (int v = t; v != s; v = pre_v[v]) {
				E& e = G[pre_v[v]][pre_i[v]];
				E& ee = G[v][e.rev];
				e.cap -= f;
				ee.cap += f;
			}
		}

		return cost;
	}
};

signed main() {
	int N; cin >> N;
	vector<int>A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> B[i];
	}

	MinCostFlow mnc(2*N+2);

	for (int i = 0; i < N; i++) {
		mnc.add_edge(0, i + 1, A[i], 0);
		mnc.add_edge(N+i+1, 2*N+1, B[i], 0);
		for (int j = 0; j < N; j++) {
			mnc.add_edge(i+1,N+j+1,A[i],abs(i-j));
		}
	}

	int ans = mnc.min_cost_flow(0,2*N+1,INF);

	cout << ans << endl;
}

Submission Info

Submission Time
Task H - WAAAAAAAAAAAAALL
User kurenai3110
Language C++14 (GCC 5.4.1)
Score 30
Code Size 2885 Byte
Status TLE
Exec Time 2201 ms
Memory 1413468 KB

Judge Result

Set Name Subtask1 Subtask2 All
Score / Max Score 30 / 30 0 / 30 0 / 140
Status
AC × 37
AC × 12
TLE × 24
AC × 52
TLE × 55
Set Name Test Cases
Subtask1 00_00_sample.txt, 00_01_sample.txt, 00_02_sample.txt, 00_03_random.txt, 00_04_random.txt, 00_05_random.txt, 00_06_random.txt, 00_07_random.txt, 00_08_random.txt, 00_09_random.txt, 00_10_random.txt, 00_11_random.txt, 00_12_random.txt, 00_13_random.txt, 00_14_random.txt, 00_15_random.txt, 00_16_random.txt, 00_17_random.txt, 00_18_random.txt, 00_19_random.txt, 00_20_random.txt, 00_21_random.txt, 00_22_random.txt, 00_23_freedom.txt, 00_24_freedom.txt, 00_25_freedom.txt, 00_26_full.txt, 00_27_full.txt, 00_28_full.txt, 00_29_min.txt, 00_30_min.txt, 00_31_min.txt, 00_32_max.txt, 00_33_max.txt, 00_34_max.txt, 00_35_max.txt, 00_36_max.txt
Subtask2 01_37_sample.txt, 01_38_sample.txt, 01_39_random.txt, 01_40_random.txt, 01_41_random.txt, 01_42_random.txt, 01_43_random.txt, 01_44_random.txt, 01_45_random.txt, 01_46_random.txt, 01_47_random.txt, 01_48_random.txt, 01_49_random.txt, 01_50_random.txt, 01_51_random.txt, 01_52_random.txt, 01_53_random.txt, 01_54_random.txt, 01_55_random.txt, 01_56_random.txt, 01_57_random.txt, 01_58_random.txt, 01_59_freedom.txt, 01_60_freedom.txt, 01_61_freedom.txt, 01_62_full.txt, 01_63_full.txt, 01_64_full.txt, 01_65_min.txt, 01_66_min.txt, 01_67_min.txt, 01_68_max.txt, 01_69_max.txt, 01_70_max.txt, 01_71_max.txt, 01_72_max.txt
All 00_00_sample.txt, 00_01_sample.txt, 00_02_sample.txt, 00_03_random.txt, 00_04_random.txt, 00_05_random.txt, 00_06_random.txt, 00_07_random.txt, 00_08_random.txt, 00_09_random.txt, 00_10_random.txt, 00_11_random.txt, 00_12_random.txt, 00_13_random.txt, 00_14_random.txt, 00_15_random.txt, 00_16_random.txt, 00_17_random.txt, 00_18_random.txt, 00_19_random.txt, 00_20_random.txt, 00_21_random.txt, 00_22_random.txt, 00_23_freedom.txt, 00_24_freedom.txt, 00_25_freedom.txt, 00_26_full.txt, 00_27_full.txt, 00_28_full.txt, 00_29_min.txt, 00_30_min.txt, 00_31_min.txt, 00_32_max.txt, 00_33_max.txt, 00_34_max.txt, 00_35_max.txt, 00_36_max.txt, 01_37_sample.txt, 01_38_sample.txt, 01_39_random.txt, 01_40_random.txt, 01_41_random.txt, 01_42_random.txt, 01_43_random.txt, 01_44_random.txt, 01_45_random.txt, 01_46_random.txt, 01_47_random.txt, 01_48_random.txt, 01_49_random.txt, 01_50_random.txt, 01_51_random.txt, 01_52_random.txt, 01_53_random.txt, 01_54_random.txt, 01_55_random.txt, 01_56_random.txt, 01_57_random.txt, 01_58_random.txt, 01_59_freedom.txt, 01_60_freedom.txt, 01_61_freedom.txt, 01_62_full.txt, 01_63_full.txt, 01_64_full.txt, 01_65_min.txt, 01_66_min.txt, 01_67_min.txt, 01_68_max.txt, 01_69_max.txt, 01_70_max.txt, 01_71_max.txt, 01_72_max.txt, 02_100_min.txt, 02_101_min.txt, 02_102_max.txt, 02_103_max.txt, 02_104_max.txt, 02_105_max.txt, 02_106_max.txt, 02_73_random.txt, 02_74_random.txt, 02_75_random.txt, 02_76_random.txt, 02_77_random.txt, 02_78_random.txt, 02_79_random.txt, 02_80_random.txt, 02_81_random.txt, 02_82_random.txt, 02_83_random.txt, 02_84_random.txt, 02_85_random.txt, 02_86_random.txt, 02_87_random.txt, 02_88_random.txt, 02_89_random.txt, 02_90_random.txt, 02_91_random.txt, 02_92_random.txt, 02_93_freedom.txt, 02_94_freedom.txt, 02_95_freedom.txt, 02_96_full.txt, 02_97_full.txt, 02_98_full.txt, 02_99_min.txt
Case Name Status Exec Time Memory
00_00_sample.txt AC 1 ms 256 KB
00_01_sample.txt AC 1 ms 256 KB
00_02_sample.txt AC 2 ms 384 KB
00_03_random.txt AC 2 ms 256 KB
00_04_random.txt AC 10 ms 512 KB
00_05_random.txt AC 9 ms 512 KB
00_06_random.txt AC 25 ms 1280 KB
00_07_random.txt AC 1 ms 256 KB
00_08_random.txt AC 9 ms 512 KB
00_09_random.txt AC 36 ms 1152 KB
00_10_random.txt AC 34 ms 1152 KB
00_11_random.txt AC 2 ms 256 KB
00_12_random.txt AC 25 ms 1024 KB
00_13_random.txt AC 1 ms 256 KB
00_14_random.txt AC 12 ms 512 KB
00_15_random.txt AC 1 ms 256 KB
00_16_random.txt AC 6 ms 512 KB
00_17_random.txt AC 5 ms 512 KB
00_18_random.txt AC 33 ms 1332 KB
00_19_random.txt AC 21 ms 1024 KB
00_20_random.txt AC 2 ms 384 KB
00_21_random.txt AC 15 ms 1024 KB
00_22_random.txt AC 1 ms 256 KB
00_23_freedom.txt AC 27 ms 1024 KB
00_24_freedom.txt AC 7 ms 512 KB
00_25_freedom.txt AC 42 ms 1152 KB
00_26_full.txt AC 5 ms 512 KB
00_27_full.txt AC 23 ms 1024 KB
00_28_full.txt AC 6 ms 512 KB
00_29_min.txt AC 1 ms 256 KB
00_30_min.txt AC 1 ms 256 KB
00_31_min.txt AC 1 ms 256 KB
00_32_max.txt AC 57 ms 1344 KB
00_33_max.txt AC 51 ms 1352 KB
00_34_max.txt AC 46 ms 1340 KB
00_35_max.txt AC 48 ms 1352 KB
00_36_max.txt AC 55 ms 1344 KB
01_37_sample.txt AC 2 ms 256 KB
01_38_sample.txt AC 1 ms 256 KB
01_39_random.txt TLE 2106 ms 44904 KB
01_40_random.txt TLE 2108 ms 76956 KB
01_41_random.txt TLE 2106 ms 61660 KB
01_42_random.txt AC 1210 ms 5516 KB
01_43_random.txt AC 1 ms 256 KB
01_44_random.txt TLE 2105 ms 43656 KB
01_45_random.txt AC 1797 ms 10344 KB
01_46_random.txt TLE 2106 ms 65896 KB
01_47_random.txt AC 8 ms 512 KB
01_48_random.txt AC 947 ms 5160 KB
01_49_random.txt TLE 2107 ms 60240 KB
01_50_random.txt TLE 2107 ms 64416 KB
01_51_random.txt AC 134 ms 1596 KB
01_52_random.txt TLE 2106 ms 53852 KB
01_53_random.txt TLE 2105 ms 38736 KB
01_54_random.txt AC 898 ms 5052 KB
01_55_random.txt TLE 2106 ms 41440 KB
01_56_random.txt TLE 2106 ms 48700 KB
01_57_random.txt TLE 2108 ms 74428 KB
01_58_random.txt TLE 2107 ms 58508 KB
01_59_freedom.txt TLE 2104 ms 18420 KB
01_60_freedom.txt TLE 2107 ms 77288 KB
01_61_freedom.txt TLE 2108 ms 75948 KB
01_62_full.txt TLE 2106 ms 42616 KB
01_63_full.txt TLE 2106 ms 39376 KB
01_64_full.txt TLE 2106 ms 36884 KB
01_65_min.txt AC 1 ms 256 KB
01_66_min.txt AC 1 ms 256 KB
01_67_min.txt AC 1 ms 256 KB
01_68_max.txt TLE 2108 ms 80728 KB
01_69_max.txt TLE 2108 ms 80728 KB
01_70_max.txt TLE 2108 ms 80716 KB
01_71_max.txt TLE 2108 ms 80744 KB
01_72_max.txt TLE 2108 ms 81028 KB
02_100_min.txt AC 1 ms 256 KB
02_101_min.txt AC 1 ms 256 KB
02_102_max.txt TLE 2165 ms 1253644 KB
02_103_max.txt TLE 2169 ms 1253348 KB
02_104_max.txt TLE 2168 ms 1317156 KB
02_105_max.txt TLE 2166 ms 1269164 KB
02_106_max.txt TLE 2166 ms 1257332 KB
02_73_random.txt TLE 2154 ms 1225716 KB
02_74_random.txt TLE 2165 ms 1274908 KB
02_75_random.txt TLE 2193 ms 1376324 KB
02_76_random.txt TLE 2161 ms 1273012 KB
02_77_random.txt TLE 2181 ms 1175076 KB
02_78_random.txt TLE 2153 ms 1218192 KB
02_79_random.txt TLE 2191 ms 1268524 KB
02_80_random.txt TLE 2163 ms 1243036 KB
02_81_random.txt TLE 2167 ms 1306684 KB
02_82_random.txt TLE 2152 ms 1206008 KB
02_83_random.txt TLE 2188 ms 1274632 KB
02_84_random.txt TLE 2192 ms 1323988 KB
02_85_random.txt TLE 2165 ms 1275012 KB
02_86_random.txt TLE 2155 ms 1237388 KB
02_87_random.txt TLE 2199 ms 1413468 KB
02_88_random.txt TLE 2159 ms 1220916 KB
02_89_random.txt TLE 2166 ms 1291404 KB
02_90_random.txt TLE 2197 ms 1367996 KB
02_91_random.txt TLE 2201 ms 1385868 KB
02_92_random.txt TLE 2190 ms 1309128 KB
02_93_freedom.txt TLE 2161 ms 1299532 KB
02_94_freedom.txt TLE 2185 ms 1234432 KB
02_95_freedom.txt TLE 2195 ms 1408028 KB
02_96_full.txt TLE 2194 ms 1265704 KB
02_97_full.txt TLE 2157 ms 1214364 KB
02_98_full.txt TLE 2160 ms 1292416 KB
02_99_min.txt AC 1 ms 256 KB