結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | fura |
提出日時 | 2020-11-28 19:55:51 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 294 ms / 3,000 ms |
コード長 | 2,452 bytes |
コンパイル時間 | 2,330 ms |
コンパイル使用メモリ | 210,168 KB |
実行使用メモリ | 44,164 KB |
最終ジャッジ日時 | 2024-09-12 23:33:09 |
合計ジャッジ時間 | 10,979 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
6,812 KB |
testcase_01 | AC | 4 ms
6,940 KB |
testcase_02 | AC | 225 ms
42,872 KB |
testcase_03 | AC | 183 ms
38,976 KB |
testcase_04 | AC | 210 ms
39,104 KB |
testcase_05 | AC | 191 ms
43,352 KB |
testcase_06 | AC | 192 ms
36,840 KB |
testcase_07 | AC | 226 ms
39,536 KB |
testcase_08 | AC | 185 ms
39,400 KB |
testcase_09 | AC | 219 ms
34,880 KB |
testcase_10 | AC | 189 ms
38,768 KB |
testcase_11 | AC | 195 ms
38,252 KB |
testcase_12 | AC | 241 ms
37,784 KB |
testcase_13 | AC | 225 ms
43,072 KB |
testcase_14 | AC | 215 ms
35,480 KB |
testcase_15 | AC | 222 ms
37,056 KB |
testcase_16 | AC | 214 ms
38,792 KB |
testcase_17 | AC | 197 ms
43,044 KB |
testcase_18 | AC | 209 ms
39,276 KB |
testcase_19 | AC | 227 ms
36,888 KB |
testcase_20 | AC | 185 ms
35,300 KB |
testcase_21 | AC | 225 ms
40,988 KB |
testcase_22 | AC | 195 ms
35,988 KB |
testcase_23 | AC | 229 ms
43,252 KB |
testcase_24 | AC | 191 ms
35,992 KB |
testcase_25 | AC | 249 ms
39,376 KB |
testcase_26 | AC | 227 ms
38,756 KB |
testcase_27 | AC | 230 ms
38,440 KB |
testcase_28 | AC | 197 ms
43,516 KB |
testcase_29 | AC | 240 ms
37,780 KB |
testcase_30 | AC | 246 ms
39,552 KB |
testcase_31 | AC | 242 ms
38,820 KB |
testcase_32 | AC | 3 ms
6,940 KB |
testcase_33 | AC | 124 ms
34,432 KB |
testcase_34 | AC | 294 ms
44,164 KB |
コンパイルメッセージ
main.cpp: In instantiation of 'void add_directed_edge(std::vector<edge<capa_t, cost_t> >*, int, int, capa_t, cost_t) [with capa_t = int; cost_t = long long int]': main.cpp:100:20: required from here main.cpp:22:57: warning: narrowing conversion of '(G + ((sizetype)(((long unsigned int)v) * 24)))->std::vector<edge<int, long long int> >::size()' from 'std::vector<edge<int, long long int> >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing] 22 | G[u].push_back((edge<capa_t,cost_t>){v,G[v].size() ,capa, cost,0}); | ~~~~~~~~~^~ main.cpp:23:59: warning: narrowing conversion of '((G + ((sizetype)(((long unsigned int)u) * 24)))->std::vector<edge<int, long long int> >::size() - 1)' from 'std::vector<edge<int, long long int> >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing] 23 | G[v].push_back((edge<capa_t,cost_t>){u,G[u].size()-1, 0,-cost,0}); | ~~~~~~~~~~~^~
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; const int V_MAX=100001; const int CAPA_INF=1<<29; const lint COST_INF=1LL<<61; template<class capa_t,class cost_t> struct edge{ int v,rev; capa_t capa; cost_t cost; capa_t flow; }; template<class capa_t,class cost_t> void add_directed_edge(vector< edge<capa_t,cost_t> > *G,int u,int v,capa_t capa,cost_t cost){ G[u].push_back((edge<capa_t,cost_t>){v,G[v].size() ,capa, cost,0}); G[v].push_back((edge<capa_t,cost_t>){u,G[u].size()-1, 0,-cost,0}); } template<class capa_t,class cost_t> pair<capa_t,cost_t> augment(int n,vector< edge<capa_t,cost_t> > *G,int s,int t,cost_t *pot){ static int pre[V_MAX]; static cost_t d[V_MAX]; rep(u,n) d[u]=(u==s?0:COST_INF); // Dijkstra bool ok=false; priority_queue< pair<cost_t,int> > Q; Q.push(make_pair(0,s)); while(!Q.empty()){ int u=Q.top().second; cost_t cost=-Q.top().first; Q.pop(); if(cost<d[u]) continue; if(u==t) ok=true; rep(i,G[u].size()){ edge<capa_t,cost_t> &e=G[u][i]; cost_t cost2=cost+e.cost+pot[u]-pot[e.v]; if(e.capa-e.flow>0 && cost2<d[e.v]){ d[e.v]=cost2; pre[e.v]=e.rev; Q.push(make_pair(-cost2,e.v)); } } } if(!ok) return make_pair(0,0); capa_t water=CAPA_INF; for(int v=t;v!=s;){ edge<capa_t,cost_t> &e1=G[v][pre[v]]; edge<capa_t,cost_t> &e2=G[e1.v][e1.rev]; water=min(water,e2.capa-e2.flow); v=e1.v; } cost_t cost=0; for(int v=t;v!=s;){ edge<capa_t,cost_t> &e1=G[v][pre[v]]; edge<capa_t,cost_t> &e2=G[e1.v][e1.rev]; e1.flow-=water; e2.flow+=water; cost+=water*e2.cost; v=e1.v; } rep(u,n) pot[u]+=d[u]; return make_pair(water,cost); } template<class capa_t,class cost_t> pair<capa_t,cost_t> primal_dual(int n,vector< edge<capa_t,cost_t> > *G,int s,int t){ static cost_t pot[V_MAX]; rep(u,n) pot[u]=0; capa_t ans1=0; cost_t ans2=0; while(1){ pair<capa_t,cost_t> tmp=augment(n,G,s,t,pot); if(tmp.first==0) break; ans1+=tmp.first; ans2+=tmp.second; } return make_pair(ans1,ans2); } int main(){ int n,m; scanf("%d%d",&n,&m); vector<edge<int,lint>> G[V_MAX]; rep(i,m){ int u,v; lint c1,c2; scanf("%d%d%lld%lld",&u,&v,&c1,&c2); u--; v--; add_directed_edge(G,u,v,1,c1); add_directed_edge(G,u,v,1,c2); add_directed_edge(G,v,u,1,c1); add_directed_edge(G,v,u,1,c2); } add_directed_edge(G,n-1,n,2,0LL); printf("%lld\n",primal_dual(n+1,G,0,n).second); return 0; }