結果
| 問題 |
No.3306 Life is Easy?
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-10-05 15:29:14 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 13,736 bytes |
| コンパイル時間 | 3,695 ms |
| コンパイル使用メモリ | 307,740 KB |
| 実行使用メモリ | 20,160 KB |
| 最終ジャッジ日時 | 2025-10-05 15:29:39 |
| 合計ジャッジ時間 | 9,794 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 34 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<(n);i++)
#define REP2(i, n) for (ll i=0;i<(n);i++)
#define REP3(i, a, n) for (ll i=a;i<(n);i++)
#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=(n)-1;i>=0;i--)
#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)
#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)
#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
ll intpow(ll a,ll b){
ll ans = 1;
while (b){
if (b & 1){
ans *= a;
}
a *= a;
b /= 2;
}
return ans;
}
ll modpow(ll a,ll b,ll c){
ll ans = 1;
while (b){
if (b & 1){
ans *= a;
ans %= c;
}
a *= a;
a %= c;
b /= 2;
}
return ans;
}
template <typename T>
inline T floor(T a, T b) {
T q = a / b;
T r = a % b;
if (r != 0 && ((r < 0) != (b < 0))) q--;
return q;
}
template <typename T>
inline T ceil(T a, T b) {
T q = a / b;
T r = a % b;
if (r != 0 && ((r < 0) == (b < 0))) q++;
return q;
}
template <typename T>
inline T bmod(T a, T b) {
T r = a % b;
if (r < 0) r += std::abs(b);
return r;
}
template <class K, class V>
vector<vector<pair<K,V>>> group_by_first(vector<pair<K,V>> vec) {
sort(vec.begin(), vec.end(), [](auto& a, auto& b) {
if (a.first != b.first) return a.first < b.first;
return a.second < b.second;
});
vector<vector<pair<K,V>>> res;
for (auto& p : vec) {
if (res.empty() || res.back().back().first != p.first) {
res.push_back({});
}
res.back().push_back(p);
}
return res;
}
template<class... T>
void input(T&... a){
(cin >> ... >> a);
}
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}
void print(){cout << "\n";}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
os << "[";
for (size_t i = 0; i < vec.size(); ++i) {
os << vec[i];
if (i + 1 < vec.size()) os << ", ";
}
os << "]";
return os;
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {
os << "[";
for (size_t j = 0; j < a.size(); ++j) {
os << "(" << a[j].first << ", " << a[j].second << ")";
if (j + 1 < a.size()) os << ", ";
}
os << "]";
return os;
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {
os << "[";
for (size_t i = 0; i < mat.size(); ++i) {
os << "[";
for (size_t j = 0; j < mat[i].size(); ++j) {
os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")";
if (j + 1 < mat[i].size()) os << ", ";
}
os << "]";
if (i + 1 < mat.size()) os << ", ";
}
os << "]";
return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
os << "{";
bool first = true;
for (const auto& x : s) {
if (!first) os << ", ";
os << x;
first = false;
}
os << "}";
return os;
}
template <typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
os << "{";
bool first = true;
for (const auto& [key, val] : m) {
if (!first) os << ", ";
os << key << ": " << val;
first = false;
}
os << "}";
return os;
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
#ifdef LOCAL
void debug() { std::cerr << "\n"; }
template<class T, class... Ts>
void debug(const T& a, const Ts&... b) {
std::cerr << a;
(std::cerr << ... << (std::cerr << ' ', b));
std::cerr << '\n';
}
#else
#define debug(...) (void)0
#endif
void write(){cout << "\n";}
template<class T, class... Ts>
void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(pll x){cout << x.first << ' ' << x.second << '\n';}
void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}}
void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
template <typename T>
T sum(const std::vector<T>& v) {
return std::accumulate(v.begin(), v.end(), T(0));
}
template<class Set>
auto le(const Set &s, const typename Set::value_type &x,
const typename Set::value_type &ngval = typename Set::value_type(-1)) {
auto it = s.upper_bound(x);
if (it == s.begin()) return ngval;
return *prev(it);
}
template<class Set>
auto lt(const Set &s, const typename Set::value_type &x,
const typename Set::value_type &ngval = typename Set::value_type(-1)) {
auto it = s.lower_bound(x);
if (it == s.begin()) return ngval;
return *prev(it);
}
template<class Set>
auto ge(const Set &s, const typename Set::value_type &x,
const typename Set::value_type &ngval = typename Set::value_type(-1)) {
auto it = s.lower_bound(x);
if (it == s.end()) return ngval;
return *it;
}
template<class Set>
auto gt(const Set &s, const typename Set::value_type &x,
const typename Set::value_type &ngval = typename Set::value_type(-1)) {
auto it = s.upper_bound(x);
if (it == s.end()) return ngval;
return *it;
}
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
#include <atcoder/mincostflow>
template <typename CapacityTp, typename CostTp> class MinCostFlow {
public:
using Edge = tuple<int, CapacityTp, CostTp, int>;
MinCostFlow(int V) {
V_ = V;
inf_cost_ = numeric_limits<CostTp>::max();
G_.resize(V_);
}
void add_edge(int u, int v, CapacityTp capacity, CostTp cost) {
G_[u].emplace_back(v, capacity, cost, G_[v].size());
G_[v].emplace_back(u, 0, -cost, G_[u].size() - 1);
}
tuple<vector<CostTp>, vector<int>, vector<int>> bellman_ford(int s) {
vector<CostTp> dist(V_, inf_cost_);
dist[s] = 0;
vector<int> pv(V_, 0);
vector<int> pe(V_, 0);
while(true) {
bool update = false;
for(int v = 0; v < V_; v++) {
if(dist[v] == inf_cost_) {
continue;
}
for(int i = 0; i < (int)G_[v].size(); i++) {
auto [nxt, capacity, cost, rev] = G_[v][i];
if(capacity > 0 && dist[nxt] > dist[v] + cost) {
dist[nxt] = dist[v] + cost;
update = true;
pv[nxt] = v;
pe[nxt] = i;
}
}
}
if(!update) {
break;
}
}
return make_tuple(dist, pv, pe);
}
CostTp calc_min_cost_flow(int s, int t, CapacityTp f) {
CostTp result = 0;
CostTp res = 0;
while(f > 0) {
auto [dist, pv, pe] = bellman_ford(s);
if(dist[t] == inf_cost_) {
return inf_cost_;
}
CapacityTp flow = f;
int v = t;
while(v != s) {
flow = min(flow, get<1>(G_[pv[v]][pe[v]]));
v = pv[v];
}
result += flow * dist[t];
debug(result,flow,f);
chmax(res,-result);
f -= flow;
v = t;
while(v != s) {
// 辺の容量を更新する
get<1>(G_[pv[v]][pe[v]]) -= flow;
// 逆辺についても同様にする
int rev = get<3>(G_[pv[v]][pe[v]]);
get<1>(G_[v][rev]) += flow;
v = pv[v];
}
}
return res;
}
private:
int V_;
vector<vector<Edge>> G_;
CostTp inf_cost_;
};
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
LL(n,m);
VVLL(a,n,m);
MinCostFlow<ll,ll> g(n*m + 2*n + 2);
ll s = n*m + 2*n;
ll t = s+1;
ll inf = 1000000000000000;
rep(i,n){
g.add_edge(s,i,1,0);
g.add_edge(n+i,t,1,0);
rep(j,m){
g.add_edge(i,2*n+i*m+j,1,0);
g.add_edge(2*n+i*m+j,n+i,1,0);
if(i+1 < n){
debug(- (a[i+1][j] - a[i][j]));
g.add_edge(2*n+i*m+j,2*n+(i+1)*m+j,n,- (a[i+1][j] - a[i][j]));
}
}
}
auto ans2 = g.calc_min_cost_flow(s,t,n);
print(ans2);
/*
auto ans = g.slope(s,t);
debug(ans);
ll w = 0;
for(auto [x,y]:ans){
chmax(w,x * inf - y);
}
print(w);
//print(ans.second - ans.first * inf);
*/
}