結果

問題 No.1301 Strange Graph Shortest Path
ユーザー okok
提出日時 2020-11-27 23:33:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 3,000 ms
コード長 2,569 bytes
コンパイル時間 1,185 ms
コンパイル使用メモリ 97,488 KB
実行使用メモリ 43,588 KB
最終ジャッジ日時 2023-10-10 21:47:45
合計ジャッジ時間 8,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 154 ms
41,720 KB
testcase_03 AC 124 ms
37,288 KB
testcase_04 AC 187 ms
39,280 KB
testcase_05 AC 132 ms
41,336 KB
testcase_06 AC 169 ms
36,796 KB
testcase_07 AC 156 ms
38,796 KB
testcase_08 AC 125 ms
37,732 KB
testcase_09 AC 153 ms
34,748 KB
testcase_10 AC 126 ms
37,296 KB
testcase_11 AC 169 ms
37,884 KB
testcase_12 AC 175 ms
37,872 KB
testcase_13 AC 153 ms
41,816 KB
testcase_14 AC 151 ms
35,100 KB
testcase_15 AC 155 ms
36,492 KB
testcase_16 AC 196 ms
39,268 KB
testcase_17 AC 163 ms
42,224 KB
testcase_18 AC 139 ms
38,072 KB
testcase_19 AC 166 ms
36,756 KB
testcase_20 AC 170 ms
35,604 KB
testcase_21 AC 158 ms
40,232 KB
testcase_22 AC 169 ms
36,616 KB
testcase_23 AC 153 ms
41,564 KB
testcase_24 AC 166 ms
36,616 KB
testcase_25 AC 179 ms
39,328 KB
testcase_26 AC 164 ms
38,016 KB
testcase_27 AC 170 ms
37,980 KB
testcase_28 AC 137 ms
41,228 KB
testcase_29 AC 173 ms
38,628 KB
testcase_30 AC 173 ms
39,436 KB
testcase_31 AC 177 ms
38,904 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 85 ms
32,152 KB
testcase_34 AC 182 ms
43,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


class mcf{
	
	struct edge{ long long to, cap, cost, rev; };
	
	long long V;
	vector<vector<edge>> G;
	vector<long long> h;
	vector<long long> dist;
	vector<long long> prevv, preve;

public :
	
	mcf(){
	}
	
	mcf(long long V): V(V){
		init(V);
	}
	
	void init(long long MAX_V){
		V = MAX_V;
		G.assign(V, {});	
	}
	
	void add_edge(long long from, long long to, long long cap, long long cost){
		G[from].push_back((edge){to, cap, cost, (long long)G[to].size()});
		G[to].push_back((edge){from, 0, -cost, (long long)G[from].size()-1});
	}
	
	long long min_cost_flow(long long s, long long t, long long f) {
		long long res = 0;
		
		prevv.assign(V, 0);		
		preve.assign(V, 0);		
		h.assign(V, 0);
		
		while(f > 0){
			priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> que;
			
			dist.assign(V, INF);	
			
			dist[s] = 0;
			
			que.emplace(0, s);
			
			while(!que.empty()){
				pair<long long, long long> p = que.top(); que.pop();
				long long v = p.second;
				
				if(dist[v] < p.first) continue;
				
				for(long long i = 0; i < G[v].size(); i++){
					edge &e = G[v][i];
					
					if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
						dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
						prevv[e.to] = v;
						preve[e.to] = i;
						que.emplace(dist[e.to], e.to);
					}
				}
			}
			
			if(dist[t] == INF) {
				return -1;
			}
			
			for(long long v = 0; v < V; v++) h[v] += dist[v];
			
			long long d = f;
			
			for(long long v = t; v != s; v = prevv[v]) {
				d = min(d, G[prevv[v]][preve[v]].cap);
			}
			
			f -= d;
			res += d * h[t];
			
			for(long long 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;
	}
};

signed main(){
	cout<<fixed<<setprecision(10);
	mcf m;
	int N, M;
    
    cin>>N>>M;
	
	m.init(N);
	
	for(int i = 0; i < M; i++){
		int u, v, c, d;
		
		cin>>u>>v>>c>>d;
		
        u--, v--;
        
		m.add_edge(u, v, 1, c);
		m.add_edge(u, v, 1, d);
        m.add_edge(v, u, 1, c);
		m.add_edge(v, u, 1, d);
	}
	
	cout<<m.min_cost_flow(0, N-1, 2)<<endl;
    
	return 0;
}
0