結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 NachiaNachia
提出日時 2020-11-28 17:25:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,572 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 210,028 KB
実行使用メモリ 45,812 KB
最終ジャッジ日時 2023-10-10 00:46:53
合計ジャッジ時間 13,909 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,768 KB
testcase_01 AC 3 ms
5,776 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 3 ms
5,720 KB
testcase_33 AC 195 ms
41,904 KB
testcase_34 AC 323 ms
44,488 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;
   int newd = d+J[j].d+fP[p]-fP[J[j].v];
   if(fD[J[j].v] != -1) continue;
   Q.push({newd,{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