結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | idat_50me |
提出日時 | 2020-12-10 01:37:18 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 236 ms / 3,000 ms |
コード長 | 5,234 bytes |
コンパイル時間 | 2,587 ms |
コンパイル使用メモリ | 215,924 KB |
実行使用メモリ | 34,604 KB |
最終ジャッジ日時 | 2024-09-19 08:04:05 |
合計ジャッジ時間 | 11,929 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 176 ms
32,948 KB |
testcase_03 | AC | 141 ms
29,472 KB |
testcase_04 | AC | 223 ms
31,488 KB |
testcase_05 | AC | 137 ms
32,336 KB |
testcase_06 | AC | 199 ms
29,360 KB |
testcase_07 | AC | 182 ms
30,656 KB |
testcase_08 | AC | 138 ms
29,776 KB |
testcase_09 | AC | 188 ms
27,904 KB |
testcase_10 | AC | 140 ms
29,224 KB |
testcase_11 | AC | 195 ms
30,344 KB |
testcase_12 | AC | 202 ms
30,160 KB |
testcase_13 | AC | 172 ms
33,156 KB |
testcase_14 | AC | 176 ms
28,132 KB |
testcase_15 | AC | 174 ms
29,072 KB |
testcase_16 | AC | 222 ms
31,488 KB |
testcase_17 | AC | 168 ms
33,292 KB |
testcase_18 | AC | 154 ms
30,252 KB |
testcase_19 | AC | 170 ms
29,440 KB |
testcase_20 | AC | 188 ms
28,544 KB |
testcase_21 | AC | 174 ms
31,868 KB |
testcase_22 | AC | 198 ms
29,312 KB |
testcase_23 | AC | 177 ms
32,700 KB |
testcase_24 | AC | 178 ms
29,056 KB |
testcase_25 | AC | 188 ms
31,360 KB |
testcase_26 | AC | 168 ms
30,432 KB |
testcase_27 | AC | 201 ms
30,524 KB |
testcase_28 | AC | 147 ms
32,372 KB |
testcase_29 | AC | 236 ms
30,848 KB |
testcase_30 | AC | 205 ms
31,404 KB |
testcase_31 | AC | 217 ms
30,976 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 85 ms
25,560 KB |
testcase_34 | AC | 203 ms
34,604 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template<typename T, typename U> using vp = vector<pair<T,U>>; template<typename T> using pque = priority_queue<T>; template<typename T> using lpque = priority_queue<T,vector<T>,greater<T>>; using ll = long long; using pint = pair<int,int>; using pll = pair<ll,ll>; using pil = pair<int,ll>; using pli = pair<ll,int>; using vint = vector<int>; using vll = vector<ll>; using qint = queue<int>; using pqint = pque<int>; using qll = queue<ll>; using pqll = pque<ll>; constexpr double PI = 3.141592653589793; constexpr int INTINF = (1<<30)-1; constexpr ll LLINF = (1LL<<62)-1; constexpr int MPRIME = 1000000007; constexpr int MPRIME9 = 998244353; constexpr ll MMPRIME = (1LL<<61)-1; constexpr char newl = '\n'; #define len length() #define pushb push_back #define fi first #define se second #define all(name) name.begin(),name.end() #define rall(name) name.rbegin(),name.rend() #define gsort(vbeg,vend) sort(vbeg,vend,greater<>()) template<typename T> struct matrix{ private: vector<vector<T>> mat; public: matrix() : matrix(0,0) {} matrix(int h, int w) { resize(h,w); } matrix(int h, int w, T init) { resize(h,w,init); } void resize(int h, int w) { mat=vector<vector<T>>(h,vector<T>(w)); } void resize(int h, int w, T init) { mat=vector<vector<T>>(h,vector<T>(w,init)); }; void in() { for(int i=0; i<mat.size(); i++) for(int j=0; j<mat[i].size(); j++) { cin>>mat[i][j]; } } void out() { for(int i=0; i<mat.size(); i++) { int wm=mat[i].size(); for(int j=0; j<wm; j++) { cout<<mat[i][j]<<(wm==j+1 ? '\n' : ' '); } } cout<<flush; } inline vector<T> &operator[](int idx) { assert(0<=idx && idx<mat.size()); return mat[idx]; } }; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline void init(T& v) { for(auto &a: v) cin>>a; } template<class T, class U> inline void init(vector<pair<T,U>>& v) { for(auto &a: v) cin>>a.first>>a.second; } template<class T, class N> inline void init(T& v, N n) { v.resize(n); for(auto &a: v) cin>>a; } template<class T, class U, class N> inline void init(vector<pair<T,U>>& v, N n) { v.resize(n); for(auto &a: v) cin>>a.first>>a.second; } template<class T> inline void out(T a) { cout<<a<<endl; } template<class T, class... U> inline void out(T a, U... alist) { cout<<a<<" "; out(forward<U>(alist)...); } template<class N> void resiz(N n) { //empty } template<class N, class T, class... U> void resiz(N n, T&& hd, U&&... tl) { hd.resize(n); resiz(n,forward<U>(tl)...); } long long binpow(long long a, long long ex, long long p=MMPRIME) { long long res = 1; while(ex > 0) { if(ex & 1) (res*=a) %= p; ex>>=1; (a*=a) %= p; } return res; } struct mincostflow { private: struct edge { int next; int rev; long long cap; long long cost; edge(int next, int rev, long long cap, long long cost) : next(next), rev(rev), cap(cap), cost(cost) {} }; public: const long long inf = (1LL<<62)-1; private: const int vnum; vector<vector<edge>> G; vector<long long> pot; vector<int> pv, pe; public: mincostflow(int N) : vnum(N), G(N), pot(N,0), pv(N), pe(N) {} void add(int from, int to, long long cap, long long cost) { assert(cost >= 0); G[from].push_back(edge(to, G[to].size(), cap, cost)); G[to].push_back(edge(from, G[from].size()-1, 0, -cost)); } private: long long dijkstra(int s, int t) { long long ans = 0; priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> q; vector<long long> dist(vnum, inf); pv.assign(vnum, -1); pe.assign(vnum, -1); q.push(make_pair(0LL,s)); dist[s] = 0; while(!q.empty()) { long long d = q.top().first, v = q.top().second; q.pop(); if(dist[v] < d) continue; for(int i=0; i<G[v].size(); i++) { edge &ed = G[v][i]; long long nd = d+ed.cost+pot[v]-pot[ed.next]; if(ed.cap>0 && dist[ed.next]>nd) { dist[ed.next] = nd; pv[ed.next] = v; pe[ed.next] = i; q.push(make_pair(nd,ed.next)); } } } if(dist[t] == inf) return inf; ans = dist[t]+pot[t]; for(int v=0; v<vnum; v++) { if(dist[v] == inf) pot[v] = inf; else pot[v] += dist[v]; } return ans; } public: // inf: 未到達 long long solve(int s, int t, int f) { long long res = 0; while(f > 0) { long long restmp = dijkstra(s, t); int add_f = f; if(restmp == inf) return inf; for(int v=t; v!=s; v=pv[v]) add_f = min((long long)add_f, G[pv[v]][pe[v]].cap); f -= add_f; res += restmp*add_f; for(int v=t; v!=s; v=pv[v]) { edge &ed = G[pv[v]][pe[v]]; ed.cap -= add_f; G[v][ed.rev].cap += add_f; } } return res; } }; int N,M; void input() { cin>>N>>M; } void solve() { mincostflow mcf(N+1); for(int i=0; i<M; i++) { int u,v,c,d; cin>>u>>v>>c>>d; mcf.add(u,v,1,c); mcf.add(u,v,1,d); mcf.add(v,u,1,c); mcf.add(v,u,1,d); } cout<<mcf.solve(1,N,2)<<newl; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(15); int t=1; while(t--) { input(); solve(); } }