結果
| 問題 | No.1301 Strange Graph Shortest Path | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-11-28 19:55:51 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 315 ms / 3,000 ms | 
| コード長 | 2,452 bytes | 
| コンパイル時間 | 2,477 ms | 
| コンパイル使用メモリ | 201,460 KB | 
| 最終ジャッジ日時 | 2025-01-16 09:45:36 | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
コンパイルメッセージ
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: warning: 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: warning: 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});
      |                                                ~~~~~~~~~~~^~
main.cpp: In function ‘int main()’:
main.cpp:95:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   95 |         int n,m; scanf("%d%d",&n,&m);
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:99:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   99 |                 lint c1,c2; scanf("%d%d%lld%lld",&u,&v,&c1,&c2); u--; v--;
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            
            ソースコード
#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;
}
            
            
            
        