結果

問題 No.1301 Strange Graph Shortest Path
ユーザー sigma425sigma425
提出日時 2020-11-28 03:18:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 4,586 bytes
コンパイル時間 2,734 ms
コンパイル使用メモリ 218,000 KB
実行使用メモリ 43,444 KB
最終ジャッジ日時 2023-10-09 23:25:10
合計ジャッジ時間 10,013 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 167 ms
41,440 KB
testcase_03 AC 137 ms
36,924 KB
testcase_04 AC 207 ms
38,848 KB
testcase_05 AC 132 ms
41,248 KB
testcase_06 AC 182 ms
36,340 KB
testcase_07 AC 163 ms
38,656 KB
testcase_08 AC 139 ms
37,580 KB
testcase_09 AC 167 ms
34,396 KB
testcase_10 AC 138 ms
37,160 KB
testcase_11 AC 175 ms
37,480 KB
testcase_12 AC 182 ms
37,320 KB
testcase_13 AC 168 ms
41,664 KB
testcase_14 AC 159 ms
34,892 KB
testcase_15 AC 154 ms
36,216 KB
testcase_16 AC 194 ms
38,892 KB
testcase_17 AC 165 ms
41,884 KB
testcase_18 AC 162 ms
37,900 KB
testcase_19 AC 177 ms
36,536 KB
testcase_20 AC 182 ms
35,240 KB
testcase_21 AC 171 ms
40,040 KB
testcase_22 AC 205 ms
36,080 KB
testcase_23 AC 182 ms
41,344 KB
testcase_24 AC 184 ms
35,840 KB
testcase_25 AC 193 ms
39,124 KB
testcase_26 AC 171 ms
38,016 KB
testcase_27 AC 186 ms
37,880 KB
testcase_28 AC 138 ms
41,156 KB
testcase_29 AC 210 ms
38,156 KB
testcase_30 AC 199 ms
38,884 KB
testcase_31 AC 198 ms
38,716 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 88 ms
32,064 KB
testcase_34 AC 193 ms
43,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct MinCostFlow{
	using C = ll;		// capacity
	using D = ll;		// cost (distance)
	const D inf = 1e18;	// max distance

	struct edge{
		int to;
		C cap;
		D cost;
		int rev;
		edge(int to_,C cap_, D cost_, int rev_):to(to_),cap(cap_),cost(cost_),rev(rev_){}
	};
	
	int N;
	VV<edge> G;
	V<D> h;
	V<D> dist;
	V<int> prevv,preve;
	MinCostFlow(int N_):N(N_){
		G.resize(N);
		h.resize(N);
		dist.resize(N);
		prevv.resize(N);
		preve.resize(N);
	}

	void add_edge(int from, int to, C cap, D cost){
		show(cap);
		show(cost);
		edge e1(to,cap,cost,(int)G[to].size());
		edge e2(from,0,-cost,(int)G[from].size());
		G[from].push_back(e1);
		G[to].push_back(e2);
	}
	D min_cost_flow(int s, int t, C f){
		D res = 0;
		h = V<D>(N);
		while(f > 0){
			using P = pair<D,int>;
			priority_queue< P,vector<P>,greater<P> > que;
			dist = V<D>(N,inf);
			dist[s] = 0;
			que.push(P(0,s));
			while(!que.empty()){
				P p = que.top();
				que.pop();
				int v = p.second;
				if(dist[v] < p.first) continue;
				for(int i=0;i<(int)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.push(P(dist[e.to],e.to));
					}
				}
			}
			if(dist[t]==inf) return -1;
			rep(v,N) h[v]+=dist[v];
			C d = f;
			for(int v=t;v!=s;v=prevv[v]){
				chmin(d,G[prevv[v]][preve[v]].cap);
			}
			f -= d;
			res += d*h[t];
			for(int 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;
	}

	/*
		流量を横軸に、コストを縦軸に取ったときのグラフ
		各線分の (dx,dy/dx) の vector を返す
		CF621 G 
	*/
	V<pair<C,D>> min_cost_flow_slopes(int s, int t){		// {(x,tan)}
		V<pair<C,D>> res;
		h = V<D>(N);
		while(true){
			using P = pair<D,int>;
			priority_queue< P,vector<P>,greater<P> > que;
			dist = V<D>(N,inf);
			dist[s] = 0;
			que.push(P(0,s));
			while(!que.empty()){
				P p = que.top();
				que.pop();
				int v = p.second;
				if(dist[v] < p.first) continue;
				for(int i=0;i<(int)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.push(P(dist[e.to],e.to));
					}
				}
			}
			if(dist[t]==inf) break;
			rep(v,N) h[v]+=dist[v];
			C f = inf;
			for(int v=t;v!=s;v=prevv[v]){
				chmin(f,G[prevv[v]][preve[v]].cap);
			}
			res.pb(f,h[t]);				// x, tan
			for(int v=t;v!=s;v=prevv[v]){
				edge &e=G[prevv[v]][preve[v]];
				e.cap-=f;
				G[v][e.rev].cap+=f;
			}
		}
		return res;
	}
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int N,M; cin >> N >> M;
	MinCostFlow MCF(N);
	rep(i,M){
		int x,y,c,d; cin >> x >> y >> c >> d; x--,y--;
		MCF.add_edge(x,y,1,c); MCF.add_edge(y,x,1,c);
		MCF.add_edge(x,y,1,d); MCF.add_edge(y,x,1,d);
	}
	cout << MCF.min_cost_flow(0,N-1,2) << endl;
}
0