結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | milanis48663220 |
提出日時 | 2020-11-28 00:37:18 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 191 ms / 3,000 ms |
コード長 | 3,406 bytes |
コンパイル時間 | 1,514 ms |
コンパイル使用メモリ | 111,448 KB |
実行使用メモリ | 43,392 KB |
最終ジャッジ日時 | 2024-07-26 21:27:17 |
合計ジャッジ時間 | 7,852 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 155 ms
41,584 KB |
testcase_03 | AC | 122 ms
37,092 KB |
testcase_04 | AC | 189 ms
38,656 KB |
testcase_05 | AC | 128 ms
41,336 KB |
testcase_06 | AC | 165 ms
36,380 KB |
testcase_07 | AC | 158 ms
38,648 KB |
testcase_08 | AC | 122 ms
37,496 KB |
testcase_09 | AC | 155 ms
34,432 KB |
testcase_10 | AC | 125 ms
36,948 KB |
testcase_11 | AC | 164 ms
37,652 KB |
testcase_12 | AC | 166 ms
37,488 KB |
testcase_13 | AC | 151 ms
41,672 KB |
testcase_14 | AC | 154 ms
34,760 KB |
testcase_15 | AC | 147 ms
36,000 KB |
testcase_16 | AC | 188 ms
38,784 KB |
testcase_17 | AC | 159 ms
41,776 KB |
testcase_18 | AC | 141 ms
38,108 KB |
testcase_19 | AC | 165 ms
36,224 KB |
testcase_20 | AC | 162 ms
35,072 KB |
testcase_21 | AC | 163 ms
39,888 KB |
testcase_22 | AC | 181 ms
35,968 KB |
testcase_23 | AC | 154 ms
41,296 KB |
testcase_24 | AC | 163 ms
35,840 KB |
testcase_25 | AC | 185 ms
38,784 KB |
testcase_26 | AC | 158 ms
37,732 KB |
testcase_27 | AC | 164 ms
37,748 KB |
testcase_28 | AC | 133 ms
41,048 KB |
testcase_29 | AC | 191 ms
37,888 KB |
testcase_30 | AC | 178 ms
39,044 KB |
testcase_31 | AC | 183 ms
38,272 KB |
testcase_32 | AC | 2 ms
6,944 KB |
testcase_33 | AC | 86 ms
31,744 KB |
testcase_34 | AC | 176 ms
43,392 KB |
コンパイルメッセージ
main.cpp: In member function 'void MinCostFlow::add_edge(int, int, ll, ll)': main.cpp:45:63: warning: narrowing conversion of '(&((MinCostFlow*)this)->MinCostFlow::G.std::vector<std::vector<edge> >::operator[](((std::vector<std::vector<edge> >::size_type)to)))->std::vector<edge>::size()' from 'std::vector<edge>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing] 45 | G[from].push_back((edge){to, cap, cost, G[to].size()}); | ~~~~~~~~~~^~ main.cpp:46:66: warning: narrowing conversion of '((&((MinCostFlow*)this)->MinCostFlow::G.std::vector<std::vector<edge> >::operator[](((std::vector<std::vector<edge> >::size_type)from)))->std::vector<edge>::size() - 1)' from 'std::vector<edge>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing] 46 | G[to].push_back((edge){from, 0, -cost, G[from].size()-1}); | ~~~~~~~~~~~~~~^~
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <cassert> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; typedef pair<ll, int> P; const ll INF = 1e18; struct edge{ int to; ll cap, cost; int rev; }; /** * 負のコストの辺がない場合のみ使える */ class MinCostFlow{ public: int V; // 頂点数 vector<vector<edge>> G; MinCostFlow(int v){ V = v; G = vector<vector<edge>>(v, vector<edge>()); dist = vector<ll>(v); h = vector<ll>(v); preve = vector<int>(v); prevv = vector<int>(v); } void add_edge(int from, int to, ll cap, ll cost){ G[from].push_back((edge){to, cap, cost, G[to].size()}); G[to].push_back((edge){from, 0, -cost, G[from].size()-1}); } ll min_cost_flow(int s, int t, int f){ ll res = 0; while(f > 0){ fill(dist.begin(), dist.end(), INF); dist[s] = 0; priority_queue<P, vector<P>, greater<P>> que; que.push(P(0, s)); while(!que.empty()){ P top = que.top(); que.pop(); int v = top.second; ll d = top.first; if(dist[v] != d) continue; for(int i = 0; i < G[v].size(); i++){ edge &e = G[v][i]; if(e.cap > 0 && dist[e.to] > dist[v]+e.cost+h[v]-h[e.to]){ dist[e.to] = dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } if(dist[t] == INF){ return -1; } for(int i = 0; i < V; i++) h[i] += dist[i]; ll d = f; for(int v = t; v != s; v = prevv[v]){ d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d*h[t]; for(int v = t; v != s; v = prevv[v]){ edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } private: vector<ll> dist, h; vector<int> prevv, preve; }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, m; cin >> n >> m; MinCostFlow mcf(n); for(int i = 0; i < m; i++){ int u, v, c, d; cin >> u >> v >> c >> d; u--; v--; mcf.add_edge(u, v, 1, c); mcf.add_edge(v, u, 1, c); mcf.add_edge(u, v, 1, d); mcf.add_edge(v, u, 1, d); } cout << mcf.min_cost_flow(0, n-1, 2) << endl; }