結果

問題 No.1301 Strange Graph Shortest Path
ユーザー rabimearabimea
提出日時 2023-01-26 18:09:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 556 ms / 3,000 ms
コード長 1,774 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 204,508 KB
実行使用メモリ 27,148 KB
最終ジャッジ日時 2023-09-09 18:05:58
合計ジャッジ時間 16,785 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,284 KB
testcase_01 AC 3 ms
8,240 KB
testcase_02 AC 366 ms
25,704 KB
testcase_03 AC 523 ms
25,244 KB
testcase_04 AC 391 ms
26,972 KB
testcase_05 AC 341 ms
25,464 KB
testcase_06 AC 361 ms
25,760 KB
testcase_07 AC 351 ms
25,612 KB
testcase_08 AC 556 ms
25,364 KB
testcase_09 AC 283 ms
25,736 KB
testcase_10 AC 304 ms
25,152 KB
testcase_11 AC 496 ms
25,812 KB
testcase_12 AC 251 ms
26,004 KB
testcase_13 AC 333 ms
25,828 KB
testcase_14 AC 361 ms
25,684 KB
testcase_15 AC 341 ms
25,536 KB
testcase_16 AC 413 ms
27,148 KB
testcase_17 AC 509 ms
26,368 KB
testcase_18 AC 442 ms
25,568 KB
testcase_19 AC 347 ms
25,788 KB
testcase_20 AC 182 ms
25,904 KB
testcase_21 AC 372 ms
25,940 KB
testcase_22 AC 197 ms
25,896 KB
testcase_23 AC 496 ms
26,060 KB
testcase_24 AC 306 ms
25,860 KB
testcase_25 AC 350 ms
26,780 KB
testcase_26 AC 419 ms
25,892 KB
testcase_27 AC 458 ms
25,768 KB
testcase_28 AC 390 ms
25,076 KB
testcase_29 AC 183 ms
26,728 KB
testcase_30 AC 465 ms
26,628 KB
testcase_31 AC 455 ms
26,572 KB
testcase_32 AC 3 ms
8,240 KB
testcase_33 AC 56 ms
26,268 KB
testcase_34 AC 285 ms
26,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pb push_back
using vi = vector <int>;
using ll = long long;
using pii = pair <int, int>;
const ll mod = 998244353;
//~ const ll mod = 1e9 + 7;
ll qpow(ll a, ll b, ll m = mod) { ll r = 1, t = a;
	for(; b; b /= 2) { if(b & 1) r = r * t % m; t = t * t % m; } return r; }

const int N = 1e5 + 11;
const int M = N * 8;

struct edge {
	int u, v, f;
	ll c;
}e[M];
int ecnt = 0;

int fe[N], ne[M];
void add(int u, int v, int f, ll c) {
	e[ecnt] = {u, v, f, c};
	ne[ecnt] = fe[u]; fe[u] = ecnt ++;
	e[ecnt] = {v, u, 0, -c};
	ne[ecnt] = fe[v]; fe[v] = ecnt ++;
}

ll dis[N];
bool inq[N];

const ll inf = 1e15;
int p[N];

int main() {
	ios :: sync_with_stdio(false);
	
	int n, m; cin >> n >> m;
	
	memset(fe, -1, sizeof fe);
	memset(ne, -1, sizeof ne);
	for(int i = 1; i <= m; i ++) {
		int u, v;
		ll c, d;
		cin >> u >> v >> c >> d;
		add(u, v, 1, c);
		add(v, u, 1, c);
		
		add(u, v, 1, d);
		add(v, u, 1, d);
	}
	
	ll ans = 0;
	for(int t : {0, 1}) {
		
		deque <int> q, rq;
		auto push = [&](int x) {
			q.push_back(x);
			rq.push_front(x);
		};
		auto pop = [&]() {
			q.pop_front();
			rq.pop_back();
		};
	 
		fill(dis, dis + n + 1, inf);
		dis[1] = 0;
		inq[1] = 1;
		push(1);
	 
		
		while(!q.empty()) {
			int x = q.front(); pop();
			inq[x] = 0;
			for(int i = fe[x]; i != -1; i = ne[i]) if(e[i].f) {
				int v = e[i].v, w = e[i].c;
				if(dis[x] + w < dis[v]) {
					dis[v] = dis[x] + w;
					p[v] = i;
					if(!inq[v]) {
						push(v);
						if(dis[q[0]] > dis[q.back()])
							q.swap(rq);
						inq[v] = 1;
					}
				}
			}
		}
		
		p[1] = -1;
		for(int i = p[n]; i != -1; i = p[e[i].u]) {
			e[i].f -= 1;
			e[i ^ 1].f += 1;
		}
		
		ans += dis[n];
	}
	cout << ans << '\n';
}
0