結果

問題 No.1301 Strange Graph Shortest Path
ユーザー okok
提出日時 2020-11-27 23:31:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,511 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 96,816 KB
実行使用メモリ 25,212 KB
最終ジャッジ日時 2023-10-10 21:47:17
合計ジャッジ時間 5,980 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 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 2 ms
4,356 KB
testcase_33 AC 68 ms
19,372 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
	}
	
	cout<<m.min_cost_flow(0, N-1, 2)<<endl;
    
	return 0;
}
0