結果

問題 No.1301 Strange Graph Shortest Path
ユーザー kzyKTkzyKT
提出日時 2020-11-28 14:50:35
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,350 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 148,036 KB
実行使用メモリ 46,332 KB
最終ジャッジ日時 2023-10-10 00:02:39
合計ジャッジ時間 17,878 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,476 KB
testcase_01 AC 4 ms
5,988 KB
testcase_02 AC 294 ms
41,892 KB
testcase_03 AC 290 ms
38,348 KB
testcase_04 AC 363 ms
38,540 KB
testcase_05 AC 278 ms
42,516 KB
testcase_06 AC 364 ms
36,440 KB
testcase_07 AC 357 ms
39,068 KB
testcase_08 AC 233 ms
38,736 KB
testcase_09 AC 316 ms
34,928 KB
testcase_10 AC 230 ms
38,264 KB
testcase_11 AC 317 ms
37,764 KB
testcase_12 AC 300 ms
37,560 KB
testcase_13 AC 316 ms
42,004 KB
testcase_14 AC 281 ms
34,996 KB
testcase_15 AC 288 ms
36,524 KB
testcase_16 AC 335 ms
38,488 KB
testcase_17 AC 358 ms
41,940 KB
testcase_18 AC 334 ms
38,844 KB
testcase_19 AC 318 ms
36,516 KB
testcase_20 AC 338 ms
35,048 KB
testcase_21 AC 294 ms
40,580 KB
testcase_22 AC 312 ms
35,760 KB
testcase_23 AC 381 ms
42,224 KB
testcase_24 AC 300 ms
35,716 KB
testcase_25 AC 327 ms
39,088 KB
testcase_26 AC 290 ms
38,356 KB
testcase_27 AC 313 ms
38,024 KB
testcase_28 AC 294 ms
42,356 KB
testcase_29 AC 375 ms
37,656 KB
testcase_30 AC 441 ms
39,064 KB
testcase_31 AC 404 ms
38,400 KB
testcase_32 AC 4 ms
6,148 KB
testcase_33 AC 91 ms
33,340 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 n,m;

ll min_cost_flow(ll s,ll t,ll f){
  ll res = 0;
  while(f>0){
    fill(dist,dist+n,MAXL); dist[s] = 0;
    bool update=true;
    while(update){
      update=false;
      for(ll v=0; v<MAX_V; v++){
        if(dist[v]==MAXL) 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() {
  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