結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 NachiaNachia
提出日時 2020-11-27 22:28:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 451 ms / 3,000 ms
コード長 1,518 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 209,584 KB
実行使用メモリ 52,296 KB
最終ジャッジ日時 2023-10-09 21:26:54
合計ジャッジ時間 17,372 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,816 KB
testcase_01 AC 2 ms
5,736 KB
testcase_02 AC 421 ms
51,872 KB
testcase_03 AC 364 ms
45,464 KB
testcase_04 AC 451 ms
46,368 KB
testcase_05 AC 401 ms
52,296 KB
testcase_06 AC 411 ms
47,668 KB
testcase_07 AC 410 ms
50,388 KB
testcase_08 AC 370 ms
46,572 KB
testcase_09 AC 395 ms
42,692 KB
testcase_10 AC 367 ms
47,688 KB
testcase_11 AC 422 ms
44,352 KB
testcase_12 AC 427 ms
45,264 KB
testcase_13 AC 418 ms
50,884 KB
testcase_14 AC 385 ms
47,308 KB
testcase_15 AC 391 ms
47,972 KB
testcase_16 AC 448 ms
44,288 KB
testcase_17 AC 443 ms
50,660 KB
testcase_18 AC 402 ms
42,340 KB
testcase_19 AC 422 ms
43,464 KB
testcase_20 AC 405 ms
42,476 KB
testcase_21 AC 428 ms
49,712 KB
testcase_22 AC 437 ms
48,684 KB
testcase_23 AC 432 ms
49,124 KB
testcase_24 AC 411 ms
42,696 KB
testcase_25 AC 447 ms
45,284 KB
testcase_26 AC 406 ms
43,164 KB
testcase_27 AC 416 ms
49,140 KB
testcase_28 AC 390 ms
48,660 KB
testcase_29 AC 436 ms
44,724 KB
testcase_30 AC 446 ms
49,420 KB
testcase_31 AC 436 ms
44,036 KB
testcase_32 AC 2 ms
5,908 KB
testcase_33 AC 264 ms
43,412 KB
testcase_34 AC 415 ms
51,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0;i<(n);i++)

const int xN = 100000;

template<class T>
using nega_queue = priority_queue<T,vector<T>,greater<T>>;

struct Edge{ int u,v,r; LL c; LL d; };

int N;
int fSrc=0;
int fSnk=1;
vector<Edge> J;
vector<int> E[xN];

LL F;
LL fAns = 0;
LL fD[xN];
int fPE[xN];
LL fP[xN]={};

void addEdge(int u,int v,LL c,LL d){
 E[u].push_back(J.size());
 J.push_back({u,v,(int)J.size()+1,c,d});
 E[v].push_back(J.size());
 J.push_back({v,u,(int)J.size()-1,0,-d});
}

void fDijkstra(){
 rep(i,N) fD[i]=fPE[i]=-1;
 nega_queue<pair<LL,pair<int,int>>> Q; Q.push({0,{fSrc,-1}});
 while(Q.size()){
  LL d=Q.top().first;
  int p=Q.top().second.first;
  int pre=Q.top().second.second;
  Q.pop();
  if(fD[p]!=-1) continue;
  fD[p]=d;
  fPE[p]=pre;
  for(int j:E[p]){
   if(J[j].c==0)continue;
   Q.push({d+J[j].d+fP[p]-fP[J[j].v],{J[j].v,j}});
  }
 }
 rep(i,N) fP[i]+=fD[i];
}

void flow(){
 fDijkstra();
 if(fD[fSnk]==-1){ F=0; fAns=-1; return; }
 int p=fSnk;
 LL c=F;
 while(p!=fSrc){
  c=min(c,J[fPE[p]].c);
  p=J[fPE[p]].u;
 }
 p=fSnk;
 while(p!=fSrc){
  J[fPE[p]].c-=c;
  J[J[fPE[p]].r].c+=c;
  fAns += c * J[fPE[p]].d;
  p=J[fPE[p]].u;
 }
 F-=c;
}

int M;

int main(){
  cin>>N>>M;
  rep(i,M){
    int u,v,c,d; cin>>u>>v>>c>>d; u--; v--;
    addEdge(u,v,1,c); addEdge(u,v,1,d);
    addEdge(v,u,1,c); addEdge(v,u,1,d);
  }

  fSrc=0; fSnk=N-1;

  F=2;
  while(F) flow();
  cout<<fAns<<endl;
  
  return 0;
}

0