結果

問題 No.1301 Strange Graph Shortest Path
ユーザー nok0nok0
提出日時 2020-10-30 23:26:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,146 bytes
コンパイル時間 3,743 ms
コンパイル使用メモリ 236,640 KB
実行使用メモリ 28,268 KB
最終ジャッジ日時 2023-10-10 21:33:31
合計ジャッジ時間 11,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 167 ms
20,796 KB
testcase_04 WA -
testcase_05 AC 169 ms
22,088 KB
testcase_06 WA -
testcase_07 AC 212 ms
23,976 KB
testcase_08 AC 159 ms
21,132 KB
testcase_09 AC 199 ms
24,020 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 208 ms
23,972 KB
testcase_14 AC 196 ms
23,400 KB
testcase_15 AC 209 ms
23,152 KB
testcase_16 AC 246 ms
27,572 KB
testcase_17 WA -
testcase_18 AC 202 ms
22,744 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 228 ms
24,228 KB
testcase_24 WA -
testcase_25 AC 227 ms
26,552 KB
testcase_26 AC 218 ms
24,220 KB
testcase_27 AC 212 ms
24,956 KB
testcase_28 AC 168 ms
22,148 KB
testcase_29 WA -
testcase_30 AC 231 ms
26,076 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 244 ms
28,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *	author: nok0
 *	created: 2020.10.30 23:14:38
**/
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for(int i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define VEC(type, name, size) \
	V<type> name(size);       \
	INPUT(name)
#define VVEC(type, name, h, w)    \
	VV<type> name(h, V<type>(w)); \
	INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LL(...)     \
	ll __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	DOUBLE __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LD(...)     \
	LD __VA_ARGS__; \
	INPUT(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(char a[]) { scanf("%s", a); }
void scan(string &a) { cin >> a; }
template <class T>
void scan(V<T> &);
template <class T, class L>
void scan(pair<T, L> &);
template <class T>
void scan(V<T> &a) {
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}
template <class T>
void scan(T &a) { cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
template <class T>
inline void print(T x) { cout << x << '\n'; }
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
	for(T &in : v) is >> in;
	return is;
}
template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
	REP(i, SZ(v)) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
//debug
template <typename T>
void view(const V<T> &v) {
	cerr << "{ ";
	for(const auto &e : v) {
		cerr << e << ", ";
	}
	cerr << "\b\b }";
}
template <typename T>
void view(const VV<T> &vv) {
	cerr << "{\n";
	for(const auto &v : vv) {
		cerr << "\t";
		view(v);
		cerr << "\n";
	}
	cerr << "}";
}
template <typename T, typename U>
void view(const V<pair<T, U>> &v) {
	cerr << "{\n";
	for(const auto &c : v) cerr << "\t(" << c.first << ", " << c.second << ")\n";
	cerr << "}";
}
template <typename T, typename U>
void view(const map<T, U> &m) {
	cerr << "{\n";
	for(auto &t : m) cerr << "\t[" << t.first << "] : " << t.second << "\n";
	cerr << "}";
}
template <typename T, typename U>
void view(const pair<T, U> &p) { cerr << "(" << p.first << ", " << p.second << ")"; }
template <typename T>
void view(const set<T> &s) {
	cerr << "{ ";
	for(auto &t : s) {
		view(t);
		cerr << ", ";
	}
	cerr << "\b\b }";
}
template <typename T>
void view(T e) { cerr << e; }
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	view(H);
	cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "\b\b]\n";                                   \
	} while(0)
#else
#define debug(...) (void(0))
#endif
template <class T>
V<T> press(V<T> &x) {
	V<T> res = x;
	sort(all(res));
	res.erase(unique(all(res)), res.end());
	REP(i, SZ(x)) {
		x[i] = lower_bound(all(res), x[i]) - res.begin();
	}
	return res;
}
template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
	if(b) {
		cout << -1 << '\n';
		exit(0);
	}
}
template <class T>
inline void fin(bool b = true, T e = 0) {
	if(b) {
		cout << e << '\n';
		exit(0);
	}
}
template <class T>
T divup(T x, T y) { return (x + (y - 1)) / y; }
template <typename T>
T pow(T a, long long n, T e = 1) {
	T ret = e;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}
struct iofast {
	iofast() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} iofast_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion

struct Edge {
	int to;
	long long cost;
	Edge() = default;
	Edge(int to_, long long cost_)
	  : to(to_), cost(cost_) {}
	bool operator<(const Edge &a) const {
		return cost < a.cost;
	}
	bool operator>(const Edge &a) const {
		return cost > a.cost;
	}
	friend std::ostream &operator<<(std::ostream &s, Edge &a) {
		s << "to:" << a.to << " cost:" << a.cost;
		return s;
	}
};

struct Graph : private std::vector<std::vector<Edge>> {
	using std::vector<std::vector<Edge>>::vector;
	using std::vector<std::vector<Edge>>::operator[];
	using std::vector<std::vector<Edge>>::operator=;
	using std::vector<std::vector<Edge>>::size;
	using std::vector<std::vector<Edge>>::resize;
	using std::vector<std::vector<Edge>>::clear;
	using std::vector<std::vector<Edge>>::push_back;
	using std::vector<std::vector<Edge>>::emplace_back;
	using std::vector<std::vector<Edge>>::begin;
	using std::vector<std::vector<Edge>>::end;

private:
	const long long INF = 1e18;

	bool isweight = true, lcainit_done = false;
	std::vector<std::vector<int>> parent;
	std::vector<int> lcadist;

	template <class T>
	inline bool chmax(T &a, T b) {
		if(a < b) {
			a = b;
			return true;
		}
		return false;
	}

	template <class T>
	inline bool chmin(T &a, T b) {
		if(a > b) {
			a = b;
			return true;
		}
		return false;
	}

public:
	void read(int e = -1, bool weight = 0, bool directed = false, int idx = 1) {
		if(e == -1) e = size() - 1;
		if(!weight) isweight = false;
		while(e--) {
			int u, v;
			long long cost = 1;
			std::cin >> u >> v;
			if(weight) std::cin >> cost;
			u -= idx, v -= idx;
			(*this)[u].emplace_back(v, cost);
			if(!directed) (*this)[v].emplace_back(u, cost);
		}
	}

	void add(int u, int v, int cost = 1, bool directed = false, int idx = 1) {
		u -= idx, v -= idx;
		(*this)[u].emplace_back(v, cost);
		if(!directed) (*this)[v].emplace_back(u, cost);
	}

	pair<ll, vector<int>> Dijkstra(int s) {
		std::vector<ll> dist(size(), INF);
		using pll = std::pair<long long, long long>;
		std::priority_queue<pll, std::vector<pll>, std::greater<pll>> que;
		dist[s] = 0;
		que.push(pll(0, s));
		std::vector<int> prever(size(), -1);
		while(!que.empty()) {
			pll p = que.top();
			que.pop();
			int v = p.second;
			if(dist[v] < p.first) continue;
			for(int i = 0; i < int((*this)[v].size()); i++) {
				Edge e = (*this)[v][i];
				if(dist[e.to] > dist[v] + e.cost) {
					dist[e.to] = dist[v] + e.cost;
					prever[e.to] = v;
					que.push(pll(dist[e.to], e.to));
				}
			}
		}
		vector<int> path;
		int t = size() - 1;
		for(; t != -1; t = prever[t]) {
			path.push_back(t);
		}
		reverse(path.begin(), path.end());
		return pair(dist[size() - 1], path);
	}

	std::vector<long long> BellmanFord(int s) {
		int n = size();
		std::vector res(n, INF);
		res[s] = 0;
		for(int loop = 0; loop < n - 1; loop++) {
			for(int v = 0; v < n; v++) {
				if(res[v] == INF) continue;
				for(auto &e : (*this)[v]) {
					res[e.to] = std::min(res[e.to], res[v] + e.cost);
				}
			}
		}

		std::queue<int> que;
		std::vector chk(n, 0);
		for(int v = 0; v < n; v++) {
			if(res[v] == INF) continue;
			for(auto &e : (*this)[v]) {
				if(res[e.to] > res[v] + e.cost) {
					res[e.to] = res[v] + e.cost;
					if(!chk[e.to]) {
						que.push(e.to);
						chk[e.to] = 1;
					}
				}
			}
		}
		while(!que.empty()) {
			int now = que.front();
			que.pop();
			for(auto &e : (*this)[now]) {
				if(!chk[e.to]) {
					chk[e.to] = 1;
					que.push(e.to);
				}
			}
		}
		for(int i = 0; i < n; i++)
			if(chk[i]) res[i] = -INF;
		return res;
	}

	std::vector<std::vector<long long>> WarshallFloyd() {
		int n = size();
		std::vector dist(n, std::vector(n, INF));
		for(int i = 0; i < n; i++) {
			dist[i][i] = 0;
		}
		for(int i = 0; i < n; i++) {
			for(auto e : (*this)[i]) {
				chmin(dist[i][e.to], e.cost);
			}
		}
		for(int k = 0; k < n; k++)
			for(int i = 0; i < n; i++)
				for(int j = 0; j < n; j++)
					chmin(dist[i][j], dist[i][k] + dist[k][j]);
		return dist;
	}

	int lca(int u, int v) {
		std::function<void(int, int, int)> dfs = [&](int now, int per, int d) {
			parent[0][now] = per;
			lcadist[now] = d;
			for(auto &e : (*this)[now])
				if(e.to != per) dfs(e.to, now, d + 1);
		};

		if(!lcainit_done) {
			lcainit_done = true;
			int n = size();
			int k = 1;
			while((1 << k) < n) k++;
			parent.assign(k, std::vector(n, -1));
			lcadist.assign(n, -1);
			dfs(0, -1, 0);
			for(int i = 0; i < k - 1; i++)
				for(int v = 0; v < n; v++) {
					if(parent[i][v] < 0)
						parent[i + 1][v] = -1;
					else
						parent[i + 1][v] = parent[i][parent[i][v]];
				}
		}
		if(lcadist[u] < lcadist[v]) std::swap(u, v);
		int k = parent.size();
		for(int i = 0; i < k; i++)
			if((lcadist[u] - lcadist[v]) >> i & 1) u = parent[i][u];
		if(u == v) return u;
		for(int i = k - 1; i >= 0; i--)
			if(parent[i][u] != parent[i][v])
				u = parent[i][u], v = parent[i][v];
		return parent[0][u];
	}

	int dist(int u, int v) { return lcadist[u] + lcadist[v] - 2 * lcadist[lca(u, v)]; }

	std::vector<int> tsort() {
		std::vector<int> res;
		int n = size();
		std::vector<int> used(n, 0);
		bool notDAG = false;

		std::function<void(int)> dfs = [&](int k) {
			if(notDAG) return;
			if(used[k]) {
				if(used[k] == 1)
					notDAG = true;
				return;
			}
			used[k] = 1;
			for(auto &e : (*this)[k]) dfs(e.to);
			used[k] = 2;
			res.push_back(k);
		};

		for(int i = 0; i < n; i++) dfs(i);
		if(notDAG) return std::vector<int>{};
		std::reverse(res.begin(), res.end());
		return res;
	}

	bool isDAG() { return tsort().size(); }

	std::vector<long long> height() {
		std::vector<long long> fir(size(), -1), sec(size(), -1);

		std::function<long long(int, int)> dfs = [&](int now, int par) {
			long long f = 0, s = 0;
			for(auto &e : (*this)[now]) {
				if(e.to == par) continue;
				s = std::max(s, dfs(e.to, now) + e.cost);
				if(f < s) std::swap(f, s);
			}
			sec[now] = s;
			return fir[now] = f;
		};

		std::function<void(int, int, long long, long long, long long)> solve = [&](int now, int par, long long parf, long long pars, long long parcost) {
			if(fir[now] + parcost == parf) parf = pars;
			sec[now] = std::max(sec[now], parf + parcost);
			if(fir[now] < sec[now]) std::swap(fir[now], sec[now]);
			for(auto &e : (*this)[now]) {
				if(e.to == par) continue;
				solve(e.to, now, fir[now], sec[now], e.cost);
			}
			return;
		};

		dfs(0, -1);
		solve(0, -1, -1, -1, -1);
		return fir;
	}

	//最小全域木
	long long Prim() {
		long long res = 0;
		std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> que;
		for(auto &e : (*this)[0])
			que.push(e);
		std::vector chk(size(), 0);
		chk[0] = 1;
		while(!que.empty()) {
			auto e = que.top();
			que.pop();
			if(chk[e.to]) continue;
			res += e.cost;
			chk[e.to] = 1;
			for(auto &e : (*this)[e.to]) {
				que.push(e);
			}
		}
		return res;
	}

	//重心
	std::vector<int> Centroid() {
		int n = size();
		std::vector<int> centroid, sz(n);

		std::function<void(int, int)> dfs = [&](int now, int per) {
			sz[now] = 1;
			bool is_centroid = true;
			for(auto &e : (*this)[now])
				if(e.to != per) {
					dfs(e.to, now);
					sz[now] += sz[e.to];
					if(sz[e.to] > n / 2) is_centroid = false;
				}
			if(n - sz[now] > n / 2) is_centroid = false;
			if(is_centroid) centroid.push_back(now);
		};

		return centroid;
	}

	//pair{橋(pairの配列),関節点(intの配列)}
	std::pair<std::vector<std::pair<int, int>>, std::vector<int>> lowlink() {
		int n = size(), k = 0;
		std::vector<int> used(n, 0), order(n, 0), low(n, 0), articulation;
		std::vector<std::pair<int, int>> bridge;

		std::function<int(int, int, int)> dfs = [&](int idx, int k, int par) {
			used[idx] = 1;
			order[idx] = k++;
			low[idx] = order[idx];
			bool is_articulation = false, neko = false;
			int cnt = 0;
			for(auto &e : (*this)[idx]) {
				if(e.to == par and !std::exchange(neko, true)) {
					continue;
				}
				if(!used[e.to]) {
					++cnt;
					k = dfs(e.to, k, idx);
					low[idx] = std::min(low[idx], low[e.to]);
					is_articulation |= par >= 0 and low[e.to] >= order[idx];
					if(order[idx] < low[e.to]) {
						auto p = std::minmax(idx, e.to);
						bridge.emplace_back(p.first, p.second);
					}
				} else {
					low[idx] = std::min(low[idx], order[e.to]);
				}
			}
			is_articulation |= par == -1 and cnt > 1;
			if(is_articulation) articulation.push_back(idx);
			return k;
		};

		for(int i = 0; i < n; i++)
			if(!used[i]) k = dfs(i, k, -1);
		return std::pair(bridge, articulation);
	}
};

ll res = 0;
int main() {
	INT(n, m);
	Graph G(n), G2(n);
	V<> u(m), v(m), c(m), d(m), used(m, 0);
	map<pair<int, int>, int> mp;
	REP(i, m) {
		cin >> u[i] >> v[i] >> c[i] >> d[i];
		G.add(u[i], v[i], c[i], 0, 1);
	}
	auto p = G.Dijkstra(0);
	res += p.first;

	REP(i, SZ(p.second) - 1) {
		mp[{p.second[i], p.second[i + 1]}] = 1;
	}
	REP(i, m) {
		if(mp[{u[i] - 1, v[i] - 1}]) {
			G2.add(u[i], v[i], d[i], 0, 1);
		} else
			G2.add(u[i], v[i], c[i], 0, 1);
	}

	res += G2.Dijkstra(0).first;

	print(res);
}
0