結果
問題 | No.119 旅行のツアーの問題 |
ユーザー |
![]() |
提出日時 | 2025-01-25 00:41:53 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 4,842 bytes |
コンパイル時間 | 2,444 ms |
コンパイル使用メモリ | 211,996 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2025-01-25 00:41:57 |
合計ジャッジ時間 | 3,823 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 19 |
ソースコード
#include<bits/stdc++.h>// #include<atcoder/all>// #include<boost/multiprecision/cpp_int.hpp>using namespace std;// using namespace atcoder;// using bint = boost::multiprecision::cpp_int;using ll = long long;using ull = unsigned long long;using ld = long double;using P = pair<ll,ll>;using vi = vector<ll>;using vvi = vector<vi>;using vvvi = vector<vvi>;using ve = vector<vector<int>>;using vb = vector<bool>;using vvb = vector<vb>;#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)#define ALL(x) (x).begin(),(x).end()#define sz(c) ((ll)(c).size())#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())// #define MOD 1000000007#define MOD 998244353template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}// https://ei1333.github.io/luzhiled/snippets/graph/dinic.htmltemplate<typename flow_t>struct Dinic{/*復元をするときはfromの頂点から出る辺からのcapを出力.is_e == trueの時その辺は元のグラフにあった"本当の"辺."本当の"辺eについてfr -> toにv[e.to][e.ref].capだけフローが流れている.二部マッチングの復元の場合,仮想の始点(終点)から(へ)の辺に注意.*/struct edge{int to;flow_t cap;int ref;//逆辺の番号bool is_e;//本物の辺かどうかint idx;//入力で与えられる辺としてのindex};vector<vector<edge>> v;vector<int> mn_cost,iter;const flow_t inf;Dinic(int n):inf(numeric_limits<flow_t>::max()),v(n){}void add_edge(int fr,int to,flow_t cap,int idx = -1){v[fr].emplace_back((edge){to,cap,(int)v[to].size(),true,idx});v[to].emplace_back((edge){fr,0,(int)v[fr].size()-1,false,idx});}bool bfs(int s,int t){mn_cost.assign(v.size(),-1);queue<int> que;mn_cost[s] = 0;que.push(s);while(!que.empty() && mn_cost[t] == -1){int ov = que.front();que.pop();for(auto &e : v[ov]){if(e.cap > 0 && mn_cost[e.to] == -1){mn_cost[e.to] = mn_cost[ov] + 1;que.push(e.to);}}}return mn_cost[t] != -1;}flow_t dfs(int ov,const int t,flow_t flow){if(ov == t)return flow;for(int &i = iter[ov];i < v[ov].size();i++){edge &e = v[ov][i];if(e.cap > 0 && mn_cost[ov] < mn_cost[e.to]){flow_t d = dfs(e.to,t,min(flow,e.cap));if(d > 0){e.cap -= d;v[e.to][e.ref].cap += d;return d;}}}return 0;}// O(m n^2)(n:頂点数 m:辺数)flow_t max_flow(int s,int t){flow_t flow = 0;while(bfs(s,t)){iter.assign(v.size(),0);flow_t f = 0;while((f = dfs(s,t,inf)) > 0)flow += f;}return flow;}void output(){for(int i = 0;i < v.size();i++){for(auto &e : v[i]) {if(!e.is_e) continue;auto &rev_e = v[e.to][e.ref];cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << "\n";}}}};int main(){ios_base::sync_with_stdio(0), cin.tie(0);int n;cin >> n;Dinic<ll> dn(2*n+2);int S = 2*n,T = S+1;const ll inf = 1ll << 40;auto ch = [](int i,int d){return 2*i+d;};// minusll res = 0;rep(i,0,n){vi th(3);cin >> th[2] >> th[0];rep(j,0,3)th[j] *= -1;res += th[2];rep(j,0,2){ll k = th[j]-th[j+1];if(k > 0){dn.add_edge(S,ch(i,j),k);}else if(k < 0){dn.add_edge(ch(i,j),T,-k);res += k;}}dn.add_edge(ch(i,1),ch(i,0),inf);}int m;cin >> m;vvi P = {{0,0,0},{0,0,0},{0,-inf,-inf}};rep(i,0,m){int d,e;cin >> d >> e;res += inf;res += -inf;dn.add_edge(ch(d,1),T,inf);// phirep(k,0,2)rep(l,0,2){ll A = P[k+1][l+1] - P[k+1][l] - P[k][l+1] + P[k][l];if(!A)continue;res += A;dn.add_edge(S,ch(d,k),-A);dn.add_edge(ch(d,k),ch(e,l),-A);}}res += dn.max_flow(S,T);cout << -res << "\n";return 0;}