結果

問題 No.1301 Strange Graph Shortest Path
ユーザー furafura
提出日時 2020-11-29 17:52:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 3,000 ms
コード長 2,409 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 212,380 KB
実行使用メモリ 34,296 KB
最終ジャッジ日時 2023-10-11 02:29:50
合計ジャッジ時間 10,252 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 171 ms
32,628 KB
testcase_03 AC 140 ms
29,120 KB
testcase_04 AC 198 ms
31,096 KB
testcase_05 AC 150 ms
32,200 KB
testcase_06 AC 188 ms
29,072 KB
testcase_07 AC 173 ms
30,600 KB
testcase_08 AC 144 ms
29,616 KB
testcase_09 AC 167 ms
27,512 KB
testcase_10 AC 144 ms
29,228 KB
testcase_11 AC 186 ms
29,832 KB
testcase_12 AC 195 ms
29,884 KB
testcase_13 AC 181 ms
32,744 KB
testcase_14 AC 171 ms
27,544 KB
testcase_15 AC 166 ms
28,916 KB
testcase_16 AC 198 ms
30,832 KB
testcase_17 AC 178 ms
33,068 KB
testcase_18 AC 157 ms
30,048 KB
testcase_19 AC 182 ms
29,112 KB
testcase_20 AC 182 ms
28,068 KB
testcase_21 AC 182 ms
31,656 KB
testcase_22 AC 182 ms
28,904 KB
testcase_23 AC 171 ms
32,580 KB
testcase_24 AC 186 ms
28,684 KB
testcase_25 AC 200 ms
30,868 KB
testcase_26 AC 184 ms
29,992 KB
testcase_27 AC 179 ms
29,996 KB
testcase_28 AC 155 ms
32,100 KB
testcase_29 AC 195 ms
30,376 KB
testcase_30 AC 189 ms
30,904 KB
testcase_31 AC 194 ms
30,460 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 99 ms
25,696 KB
testcase_34 AC 223 ms
34,296 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);

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

			// Dijkstra
			bool ok=false;
			priority_queue<pair<cost_t,int>> Q; 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