結果

問題 No.1301 Strange Graph Shortest Path
ユーザー leaf_1415leaf_1415
提出日時 2020-11-27 22:56:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,037 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 113,432 KB
実行使用メモリ 52,788 KB
最終ジャッジ日時 2023-10-09 21:51:00
合計ジャッジ時間 27,301 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
22,904 KB
testcase_01 AC 8 ms
22,772 KB
testcase_02 AC 638 ms
52,292 KB
testcase_03 AC 545 ms
49,088 KB
testcase_04 AC 802 ms
51,460 KB
testcase_05 AC 549 ms
52,272 KB
testcase_06 AC 689 ms
49,736 KB
testcase_07 AC 639 ms
51,224 KB
testcase_08 AC 533 ms
49,612 KB
testcase_09 AC 632 ms
48,212 KB
testcase_10 AC 515 ms
49,416 KB
testcase_11 AC 690 ms
50,696 KB
testcase_12 AC 695 ms
50,576 KB
testcase_13 AC 647 ms
52,312 KB
testcase_14 AC 619 ms
48,568 KB
testcase_15 AC 634 ms
49,680 KB
testcase_16 AC 774 ms
51,332 KB
testcase_17 AC 740 ms
52,788 KB
testcase_18 AC 613 ms
49,880 KB
testcase_19 AC 684 ms
50,148 KB
testcase_20 AC 752 ms
48,992 KB
testcase_21 AC 682 ms
51,856 KB
testcase_22 AC 748 ms
49,564 KB
testcase_23 AC 696 ms
52,320 KB
testcase_24 AC 718 ms
49,432 KB
testcase_25 AC 730 ms
52,004 KB
testcase_26 AC 634 ms
50,872 KB
testcase_27 AC 697 ms
50,784 KB
testcase_28 AC 558 ms
51,504 KB
testcase_29 AC 841 ms
50,992 KB
testcase_30 AC 749 ms
51,544 KB
testcase_31 AC 734 ms
51,320 KB
testcase_32 AC 8 ms
22,736 KB
testcase_33 TLE -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e15
#define mod 998244353

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct edge{
	int to, cap, cost, rev;
	edge(){}
	edge(llint a, llint b, llint c, llint d){
		to = a, cap = b, cost = c, rev = d;
	}
};

int n, m, F;
int S, T;
vector<edge> G[500005];
llint dist[500005];
int prevv[500005], preve[500005];
llint h[500005];

void BellmanFord()
{
	for(int i = 0; i <= T; i++) dist[i] = inf;
	dist[S] = 0, prevv[S] = -1;
	
	bool update = true;
	while(update){
		update = false;
		for(int i = 0; i <= T; i++){
			for(int j = 0; j < G[i].size(); j++){
				if(G[i][j].cap == 0) continue;
				if(dist[G[i][j].to] > dist[i] + G[i][j].cost){
					dist[G[i][j].to] = dist[i] + G[i][j].cost;
					prevv[G[i][j].to] = i;
					preve[G[i][j].to] = j;
					update = true;
				}
			}
		}
	}
}

void Dijkstra()
{
	for(int i = 0; i <= T; i++) dist[i] = inf;
	dist[S] = 0, prevv[S] = -1;
	
	priority_queue< P, vector<P>, greater<P> > Q;
	Q.push( make_pair(0, S) );
	
	llint v, d;
	while(Q.size()){
		d = Q.top().first;
		v = Q.top().second;
		Q.pop();
		if(dist[v] < d) continue;
		for(int i = 0; i < G[v].size(); i++){
			if(G[v][i].cap == 0) continue;
			llint u = G[v][i].to, c = h[v] - h[u] + G[v][i].cost;
			if(dist[u] > d + c){
				dist[u] = d + c;
				prevv[u] = v;
				preve[u] = i;
				Q.push( make_pair(dist[u], u) );
			}
		}
	}
}

void add_edge(llint from, llint to, llint cap, llint cost)
{
	G[from].push_back( edge(to, cap, cost, G[to].size()) );
	G[to].push_back( edge(from, 0, -cost, G[from].size()-1) );
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	ll u, v, c, d;
	rep(i, 1, m){
		cin >> u >> v >> c >> d ;
		add_edge(u, n+i, 2, 0);
		add_edge(v, n+i, 2, 0);
		add_edge(n+i, n+m+i, 1, c);
		add_edge(n+i, n+m+i, 1, d);
		add_edge(n+m+i, u, 2, 0);
		add_edge(n+m+i, v, 2, 0);
	}
	S = 1, T = n+2*m+1;
	add_edge(n, T, 2, 0);
	
	BellmanFord();
	for(int i = 0; i <= T; i++) h[i] = dist[i];
	
	int f = 2; ll ans = 0;
	while(f > 0){
		Dijkstra();
		if(dist[T] >= inf) break;
		
		int p = T, flow = f;
		while(prevv[p] != -1){
			flow = min(flow, G[prevv[p]][preve[p]].cap);
			p = prevv[p];
		}
		
		p = T;
		while(prevv[p] != -1){
			G[prevv[p]][preve[p]].cap -= flow;
			G[p][G[prevv[p]][preve[p]].rev].cap += flow;
			p = prevv[p];
		}
		f -= flow;
		ans += (dist[T] + h[T] - h[S]) * flow;
		
		for(int i = 0; i <= T; i++) h[i] += dist[i]; //オーバーフローに注意(?)
	}
	
	if(f > 0) ans = -1;
	cout << ans << endl;
	
	
	return 0;
}
0