結果

問題 No.1301 Strange Graph Shortest Path
ユーザー kzyKTkzyKT
提出日時 2020-11-27 22:16:30
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,355 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 147,640 KB
実行使用メモリ 42,736 KB
最終ジャッジ日時 2023-10-09 21:19:47
合計ジャッジ時間 21,278 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,440 KB
testcase_01 AC 4 ms
6,476 KB
testcase_02 AC 318 ms
41,720 KB
testcase_03 AC 295 ms
38,724 KB
testcase_04 AC 458 ms
38,688 KB
testcase_05 AC 273 ms
42,736 KB
testcase_06 AC 454 ms
36,280 KB
testcase_07 AC 385 ms
39,160 KB
testcase_08 AC 245 ms
38,904 KB
testcase_09 AC 368 ms
34,728 KB
testcase_10 AC 234 ms
38,088 KB
testcase_11 AC 380 ms
37,436 KB
testcase_12 AC 378 ms
37,588 KB
testcase_13 AC 347 ms
42,060 KB
testcase_14 AC 317 ms
35,072 KB
testcase_15 AC 339 ms
36,816 KB
testcase_16 AC 439 ms
38,504 KB
testcase_17 AC 404 ms
41,968 KB
testcase_18 AC 366 ms
38,936 KB
testcase_19 AC 384 ms
36,400 KB
testcase_20 AC 470 ms
34,984 KB
testcase_21 AC 343 ms
40,552 KB
testcase_22 AC 412 ms
35,784 KB
testcase_23 AC 418 ms
42,236 KB
testcase_24 AC 387 ms
35,768 KB
testcase_25 AC 406 ms
39,020 KB
testcase_26 AC 333 ms
38,332 KB
testcase_27 AC 364 ms
37,940 KB
testcase_28 AC 310 ms
42,532 KB
testcase_29 AC 488 ms
37,608 KB
testcase_30 AC 489 ms
39,116 KB
testcase_31 AC 480 ms
38,236 KB
testcase_32 AC 4 ms
6,440 KB
testcase_33 AC 94 ms
33,392 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;


const int MAX_V= 100002;
 
struct edge{
  ll to,cap,cost,rev;
  edge(ll to,ll cap,ll cost,ll rev) :
    to(to),cap(cap),cost(cost),rev(rev) {}
};
  
vector<edge> G[MAX_V];
ll dist[MAX_V];
ll prevv[MAX_V],preve[MAX_V];
  
void add_edge(ll from,ll 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(ll s,ll t,ll f){
  ll res = 0;
  while(f>0){
    fill(dist,dist+MAX_V,MAXL); dist[s] = 0;
    bool update=true;
    while(update){
      update=false;
      for(ll v=0; v<MAX_V; v++){
        if(dist[v]==MAX) continue;
        for(ll i=0; i<(int)G[v].size(); i++){
          edge &e=G[v][i];
          if(e.cap>0 && dist[e.to]>dist[v]+e.cost){
            dist[e.to]=dist[v]+e.cost;
            prevv[e.to]=v; preve[e.to]=i; update=true;
          }
        }
      }
    }
    if(dist[t]==MAXL) return -1;
    ll d = f;
    for(ll v=t; v!=s; v=prevv[v]){
      d=min(d,G[prevv[v]][preve[v]].cap);
    }
    f-=d;res+=d*dist[t];
    for(ll 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;
}

void Main() {
  ll n,m;
  cin >> n >> m;
  rep(i,m) {
    ll x,y,z1,z2;
    cin >> x >> y >> z1 >> z2;
    x--,y--;
    add_edge(x,y,1,z1);
    add_edge(y,x,1,z1);
    add_edge(x,y,1,z2);
    add_edge(y,x,1,z2);
  }
  pr(min_cost_flow(0,n-1,2));
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
0