結果

問題 No.1301 Strange Graph Shortest Path
ユーザー furafura
提出日時 2020-11-28 19:55:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 3,000 ms
コード長 2,452 bytes
コンパイル時間 2,527 ms
コンパイル使用メモリ 208,228 KB
実行使用メモリ 43,748 KB
最終ジャッジ日時 2023-10-10 01:02:57
合計ジャッジ時間 12,618 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,048 KB
testcase_01 AC 2 ms
5,800 KB
testcase_02 AC 260 ms
42,676 KB
testcase_03 AC 203 ms
38,828 KB
testcase_04 AC 242 ms
39,088 KB
testcase_05 AC 216 ms
43,144 KB
testcase_06 AC 205 ms
36,496 KB
testcase_07 AC 257 ms
39,548 KB
testcase_08 AC 216 ms
39,256 KB
testcase_09 AC 251 ms
34,980 KB
testcase_10 AC 219 ms
38,604 KB
testcase_11 AC 212 ms
37,928 KB
testcase_12 AC 276 ms
37,556 KB
testcase_13 AC 249 ms
42,916 KB
testcase_14 AC 254 ms
35,384 KB
testcase_15 AC 251 ms
36,988 KB
testcase_16 AC 240 ms
38,620 KB
testcase_17 AC 231 ms
42,716 KB
testcase_18 AC 235 ms
39,124 KB
testcase_19 AC 272 ms
36,784 KB
testcase_20 AC 212 ms
34,956 KB
testcase_21 AC 260 ms
40,892 KB
testcase_22 AC 207 ms
35,880 KB
testcase_23 AC 260 ms
43,000 KB
testcase_24 AC 207 ms
35,924 KB
testcase_25 AC 285 ms
39,196 KB
testcase_26 AC 261 ms
38,340 KB
testcase_27 AC 267 ms
38,092 KB
testcase_28 AC 225 ms
43,292 KB
testcase_29 AC 290 ms
37,620 KB
testcase_30 AC 300 ms
39,248 KB
testcase_31 AC 299 ms
38,748 KB
testcase_32 AC 4 ms
5,820 KB
testcase_33 AC 135 ms
34,228 KB
testcase_34 AC 343 ms
43,748 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘void add_directed_edge(std::vector<edge<capa_t, cost_t> >*, int, int, capa_t, cost_t) [with capa_t = int; cost_t = long long int]’:
main.cpp:100:20:   required from here
main.cpp:22:57: 警告: narrowing conversion of ‘(G + ((sizetype)(((long unsigned int)v) * 24)))->std::vector<edge<int, long long int> >::size()’ from ‘std::vector<edge<int, long long int> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   22 |         G[u].push_back((edge<capa_t,cost_t>){v,G[v].size()  ,capa, cost,0});
      |                                                ~~~~~~~~~^~
main.cpp:23:59: 警告: narrowing conversion of ‘((G + ((sizetype)(((long unsigned int)u) * 24)))->std::vector<edge<int, long long int> >::size() - 1)’ from ‘std::vector<edge<int, long long int> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   23 |         G[v].push_back((edge<capa_t,cost_t>){u,G[u].size()-1,   0,-cost,0});
      |                                                ~~~~~~~~~~~^~

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;
using lint=long long;

const int V_MAX=100001;
const int CAPA_INF=1<<29;
const lint COST_INF=1LL<<61;

template<class capa_t,class cost_t>
struct edge{
	int v,rev;
	capa_t capa;
	cost_t cost;
	capa_t flow;
};

template<class capa_t,class cost_t>
void add_directed_edge(vector< edge<capa_t,cost_t> > *G,int u,int v,capa_t capa,cost_t cost){
	G[u].push_back((edge<capa_t,cost_t>){v,G[v].size()  ,capa, cost,0});
	G[v].push_back((edge<capa_t,cost_t>){u,G[u].size()-1,   0,-cost,0});
}

template<class capa_t,class cost_t>
pair<capa_t,cost_t> augment(int n,vector< edge<capa_t,cost_t> > *G,int s,int t,cost_t *pot){
	static int pre[V_MAX];
	static cost_t d[V_MAX];
	rep(u,n) d[u]=(u==s?0:COST_INF);

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

		rep(i,G[u].size()){
			edge<capa_t,cost_t> &e=G[u][i];
			cost_t cost2=cost+e.cost+pot[u]-pot[e.v];
			if(e.capa-e.flow>0 && cost2<d[e.v]){
				d[e.v]=cost2;
				pre[e.v]=e.rev;
				Q.push(make_pair(-cost2,e.v));
			}
		}
	}

	if(!ok) return make_pair(0,0);

	capa_t water=CAPA_INF;
	for(int v=t;v!=s;){
		edge<capa_t,cost_t> &e1=G[v][pre[v]];
		edge<capa_t,cost_t> &e2=G[e1.v][e1.rev];
		water=min(water,e2.capa-e2.flow);
		v=e1.v;
	}

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

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

	return make_pair(water,cost);
}

template<class capa_t,class cost_t>
pair<capa_t,cost_t> primal_dual(int n,vector< edge<capa_t,cost_t> > *G,int s,int t){
	static cost_t pot[V_MAX];
	rep(u,n) pot[u]=0;

	capa_t ans1=0;
	cost_t ans2=0;
	while(1){
		pair<capa_t,cost_t> tmp=augment(n,G,s,t,pot);
		if(tmp.first==0) break;
		ans1+=tmp.first;
		ans2+=tmp.second;
	}
	return make_pair(ans1,ans2);
}

int main(){
	int n,m; scanf("%d%d",&n,&m);
	vector<edge<int,lint>> G[V_MAX];
	rep(i,m){
		int u,v;
		lint c1,c2; scanf("%d%d%lld%lld",&u,&v,&c1,&c2); u--; v--;
		add_directed_edge(G,u,v,1,c1);
		add_directed_edge(G,u,v,1,c2);
		add_directed_edge(G,v,u,1,c1);
		add_directed_edge(G,v,u,1,c2);
	}
	add_directed_edge(G,n-1,n,2,0LL);
	printf("%lld\n",primal_dual(n+1,G,0,n).second);

	return 0;
}
0