結果

問題 No.1301 Strange Graph Shortest Path
ユーザー idat_50meidat_50me
提出日時 2020-12-10 01:37:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 3,000 ms
コード長 5,234 bytes
コンパイル時間 5,197 ms
コンパイル使用メモリ 214,800 KB
実行使用メモリ 34,564 KB
最終ジャッジ日時 2023-10-19 11:59:21
合計ジャッジ時間 13,705 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 158 ms
32,948 KB
testcase_03 AC 126 ms
29,320 KB
testcase_04 AC 205 ms
31,512 KB
testcase_05 AC 127 ms
32,312 KB
testcase_06 AC 178 ms
29,408 KB
testcase_07 AC 163 ms
30,768 KB
testcase_08 AC 132 ms
29,884 KB
testcase_09 AC 172 ms
27,944 KB
testcase_10 AC 137 ms
29,468 KB
testcase_11 AC 188 ms
30,324 KB
testcase_12 AC 190 ms
30,272 KB
testcase_13 AC 162 ms
33,156 KB
testcase_14 AC 169 ms
28,116 KB
testcase_15 AC 166 ms
29,056 KB
testcase_16 AC 209 ms
31,472 KB
testcase_17 AC 178 ms
33,288 KB
testcase_18 AC 154 ms
30,364 KB
testcase_19 AC 188 ms
29,516 KB
testcase_20 AC 190 ms
28,572 KB
testcase_21 AC 170 ms
31,848 KB
testcase_22 AC 202 ms
29,364 KB
testcase_23 AC 175 ms
32,812 KB
testcase_24 AC 197 ms
29,224 KB
testcase_25 AC 207 ms
31,456 KB
testcase_26 AC 170 ms
30,416 KB
testcase_27 AC 186 ms
30,384 KB
testcase_28 AC 137 ms
32,236 KB
testcase_29 AC 215 ms
30,948 KB
testcase_30 AC 190 ms
31,392 KB
testcase_31 AC 193 ms
31,056 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 84 ms
25,520 KB
testcase_34 AC 183 ms
34,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T, typename U>
using vp = vector<pair<T,U>>;
template<typename T>
using pque = priority_queue<T>;
template<typename T>
using lpque = priority_queue<T,vector<T>,greater<T>>;

using ll = long long;
using pint = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using vint = vector<int>;
using vll = vector<ll>;
using qint = queue<int>;
using pqint = pque<int>;
using qll = queue<ll>;
using pqll = pque<ll>;

constexpr double PI = 3.141592653589793;
constexpr int INTINF = (1<<30)-1;
constexpr ll LLINF = (1LL<<62)-1;
constexpr int MPRIME = 1000000007;
constexpr int MPRIME9 = 998244353;
constexpr ll MMPRIME = (1LL<<61)-1;
constexpr char newl = '\n';

#define len length()
#define pushb push_back
#define fi first
#define se second

#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())

template<typename T>
struct matrix{
private:
	vector<vector<T>> mat;

public:
	matrix() : matrix(0,0) {}
	matrix(int h, int w) { resize(h,w); }
	matrix(int h, int w, T init) { resize(h,w,init); }

	void resize(int h, int w) {
		mat=vector<vector<T>>(h,vector<T>(w));
	}
	void resize(int h, int w, T init) {
		mat=vector<vector<T>>(h,vector<T>(w,init));
	};

	void in() {
		for(int i=0; i<mat.size(); i++) for(int j=0; j<mat[i].size(); j++) {
			cin>>mat[i][j];
		}
	}

	void out() {
		for(int i=0; i<mat.size(); i++) {
			int wm=mat[i].size();
			for(int j=0; j<wm; j++) {
				cout<<mat[i][j]<<(wm==j+1 ? '\n' : ' ');
			}
		}
		cout<<flush;
	}

	inline vector<T> &operator[](int idx) {
		assert(0<=idx && idx<mat.size());
		return mat[idx];
	}
};

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;
}

template<class T>
inline void init(T& v) {
	for(auto &a: v) cin>>a;
}
template<class T, class U>
inline void init(vector<pair<T,U>>& v) {
	for(auto &a: v) cin>>a.first>>a.second;
}
template<class T, class N>
inline void init(T& v, N n) {
	v.resize(n);
	for(auto &a: v) cin>>a;
}
template<class T, class U, class N>
inline void init(vector<pair<T,U>>& v, N n) {
	v.resize(n);
	for(auto &a: v) cin>>a.first>>a.second;
}

template<class T>
inline void out(T a) {
	cout<<a<<endl;
}
template<class T, class... U>
inline void out(T a, U... alist) {
	cout<<a<<" ";
	out(forward<U>(alist)...);
}

template<class N>
void resiz(N n) {
	//empty
}
template<class N, class T, class... U>
void resiz(N n, T&& hd, U&&... tl) {
	hd.resize(n);
	resiz(n,forward<U>(tl)...);
}

long long binpow(long long a, long long ex, long long p=MMPRIME) {
	long long res = 1;
	while(ex > 0) {
		if(ex & 1) (res*=a) %= p;
		ex>>=1;
		(a*=a) %= p;
	}
	return res;
}


struct mincostflow {
private:
	struct edge {
		int next;
		int rev;
		long long cap;
		long long cost;
		
		edge(int next, int rev, long long cap, long long cost) : next(next), rev(rev), cap(cap), cost(cost) {}
	};

public:
	const long long inf = (1LL<<62)-1;

private:
	const int vnum;
	vector<vector<edge>> G;
	vector<long long> pot;
	vector<int> pv, pe;

public:
	mincostflow(int N) : vnum(N), G(N), pot(N,0), pv(N), pe(N) {}

	void add(int from, int to, long long cap, long long cost) {
		assert(cost >= 0);
		G[from].push_back(edge(to, G[to].size(), cap, cost));
		G[to].push_back(edge(from, G[from].size()-1, 0, -cost));
	}

private:
	long long dijkstra(int s, int t) {
		long long ans = 0;
		priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> q;
		vector<long long> dist(vnum, inf);
		pv.assign(vnum, -1);
		pe.assign(vnum, -1);
		q.push(make_pair(0LL,s));
		dist[s] = 0;

		while(!q.empty()) {
			long long d = q.top().first, v = q.top().second;
			q.pop();
			if(dist[v] < d) continue;
			for(int i=0; i<G[v].size(); i++) {
				edge &ed = G[v][i];
				long long nd = d+ed.cost+pot[v]-pot[ed.next];
				if(ed.cap>0 && dist[ed.next]>nd) {
					dist[ed.next] = nd;
					pv[ed.next] = v;
					pe[ed.next] = i;
					q.push(make_pair(nd,ed.next));
				}
			}
		}

		if(dist[t] == inf) return inf;

		ans = dist[t]+pot[t];
		for(int v=0; v<vnum; v++) {
			if(dist[v] == inf) pot[v] = inf;
			else pot[v] += dist[v];
		}
		return ans;
	}

public:
	// inf: 未到達
	long long solve(int s, int t, int f) {
		long long res = 0;

		while(f > 0) {
			long long restmp = dijkstra(s, t);
			int add_f = f;
			if(restmp == inf) return inf;
			for(int v=t; v!=s; v=pv[v]) add_f = min((long long)add_f, G[pv[v]][pe[v]].cap);
			f -= add_f;
			res += restmp*add_f;
			for(int v=t; v!=s; v=pv[v]) {
				edge &ed = G[pv[v]][pe[v]];
				ed.cap -= add_f;
				G[v][ed.rev].cap += add_f;
			}
		}
		return res;
	}
};



int N,M;

void input() {
	cin>>N>>M;
}

void solve() {
	mincostflow mcf(N+1);
	for(int i=0; i<M; i++) {
		int u,v,c,d; cin>>u>>v>>c>>d;
		mcf.add(u,v,1,c);
		mcf.add(u,v,1,d);
		mcf.add(v,u,1,c);
		mcf.add(v,u,1,d);
	}

	cout<<mcf.solve(1,N,2)<<newl;
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(15);
	int t=1;
	while(t--) {
		input();
		solve();
	}
}
0