結果

問題 No.1301 Strange Graph Shortest Path
ユーザー furafura
提出日時 2020-11-29 18:35:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 3,000 ms
コード長 2,411 bytes
コンパイル時間 2,435 ms
コンパイル使用メモリ 213,940 KB
実行使用メモリ 34,580 KB
最終ジャッジ日時 2023-10-11 02:30:43
合計ジャッジ時間 10,005 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 162 ms
32,096 KB
testcase_03 AC 131 ms
28,720 KB
testcase_04 AC 193 ms
31,064 KB
testcase_05 AC 136 ms
31,676 KB
testcase_06 AC 168 ms
28,948 KB
testcase_07 AC 167 ms
30,076 KB
testcase_08 AC 139 ms
29,196 KB
testcase_09 AC 174 ms
27,576 KB
testcase_10 AC 149 ms
28,632 KB
testcase_11 AC 180 ms
29,996 KB
testcase_12 AC 178 ms
29,684 KB
testcase_13 AC 166 ms
32,288 KB
testcase_14 AC 165 ms
27,588 KB
testcase_15 AC 169 ms
28,508 KB
testcase_16 AC 199 ms
31,016 KB
testcase_17 AC 176 ms
32,396 KB
testcase_18 AC 152 ms
29,580 KB
testcase_19 AC 179 ms
29,008 KB
testcase_20 AC 178 ms
27,948 KB
testcase_21 AC 174 ms
31,152 KB
testcase_22 AC 180 ms
28,780 KB
testcase_23 AC 166 ms
32,244 KB
testcase_24 AC 177 ms
28,612 KB
testcase_25 AC 208 ms
31,000 KB
testcase_26 AC 169 ms
29,812 KB
testcase_27 AC 178 ms
29,744 KB
testcase_28 AC 153 ms
31,716 KB
testcase_29 AC 203 ms
30,492 KB
testcase_30 AC 195 ms
30,916 KB
testcase_31 AC 193 ms
30,596 KB
testcase_32 AC 1 ms
4,372 KB
testcase_33 AC 98 ms
25,812 KB
testcase_34 AC 216 ms
34,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class capa_t,class cost_t>
class mcf_graph{
	struct edge{
		int to,rev;
		capa_t capa,flow;
		cost_t cost;
		edge(int to,int rev,const capa_t& capa,const cost_t& cost,const capa_t& flow):to(to),rev(rev),capa(capa),cost(cost),flow(flow){}
	};

	vector<vector<edge>> G;

	static constexpr capa_t CAPA_INF=numeric_limits<capa_t>::max();
	static constexpr cost_t COST_INF=numeric_limits<cost_t>::max();

public:
	mcf_graph(){}
	mcf_graph(int n):G(n){}

	void add_directed_edge(int u,int v,const capa_t& capa,const cost_t& cost){
		G[u].emplace_back(v,G[v].size()  ,capa, cost,0);
		G[v].emplace_back(u,G[u].size()-1,   0,-cost,0);
	}

	pair<capa_t,cost_t> minimum_cost_flow(int s,int t,capa_t limit=CAPA_INF){
		int n=G.size();
		vector<int> pre(n);
		vector<cost_t> d(n),pot(n);
		priority_queue<pair<cost_t,int>> Q;

		auto augment=[&]()->pair<capa_t,cost_t>{
			rep(u,n) d[u]=(u==s?0:COST_INF);

			// Dijkstra
			bool ok=false;
			Q.emplace(0,s);
			while(!Q.empty()){
				int u=Q.top().second;
				cost_t cost=-Q.top().first; Q.pop();

				if(cost<d[u]) continue;
				if(u==t) ok=true;

				for(const edge& e:G[u]) if(e.capa-e.flow>0) {
					cost_t cost2=cost+e.cost+pot[u]-pot[e.to];
					if(cost2<d[e.to]){
						d[e.to]=cost2;
						pre[e.to]=e.rev;
						Q.emplace(-cost2,e.to);
					}
				}
			}

			if(!ok) return {0,0};

			capa_t water=limit;
			for(int u=t;u!=s;){
				edge& e1=G[u][pre[u]];
				edge& e2=G[e1.to][e1.rev];
				water=min(water,e2.capa-e2.flow);
				u=e1.to;
			}
			limit-=water;

			cost_t cost=0;
			for(int u=t;u!=s;){
				edge& e1=G[u][pre[u]];
				edge& e2=G[e1.to][e1.rev];
				e1.flow-=water;
				e2.flow+=water;
				cost+=water*e2.cost;
				u=e1.to;
			}

			rep(u,n) pot[u]+=d[u];

			return {water,cost};
		};

		capa_t res1=0;
		cost_t res2=0;
		while(limit>0){
			auto tmp=augment();
			if(tmp.first==0) break;
			res1+=tmp.first;
			res2+=tmp.second;
		}
		return make_pair(res1,res2);
	}
};

int main(){
	int n,m; scanf("%d%d",&n,&m);

	mcf_graph<int,lint> G(n);
	rep(i,m){
		int u,v;
		lint c1,c2; scanf("%d%d%lld%lld",&u,&v,&c1,&c2); u--; v--;
		G.add_directed_edge(u,v,1,c1);
		G.add_directed_edge(u,v,1,c2);
		G.add_directed_edge(v,u,1,c1);
		G.add_directed_edge(v,u,1,c2);
	}

	printf("%lld\n",G.minimum_cost_flow(0,n-1,2).second);

	return 0;
}
0