結果
| 問題 |
No.119 旅行のツアーの問題
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-09-05 19:52:46 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 5,000 ms |
| コード長 | 1,943 bytes |
| コンパイル時間 | 2,634 ms |
| コンパイル使用メモリ | 205,400 KB |
| 最終ジャッジ日時 | 2025-01-24 08:25:27 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 19 |
コンパイルメッセージ
main.cpp: In instantiation of ‘int MaxFlowGraph<Cap>::add_edge(int, int, Cap) [with Cap = int]’:
main.cpp:67:15: required from here
main.cpp:14:37: warning: narrowing conversion of ‘(&((MaxFlowGraph<int>*)this)->MaxFlowGraph<int>::G.std::vector<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >, std::allocator<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> > > >::operator[](((std::vector<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >, std::allocator<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> > > >::size_type)b)))->std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >::size()’ from ‘std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
14 | G[a].push_back({b, G[b].size(), edg.size(), c});
| ~~~~~~~~~^~
main.cpp:14:49: warning: narrowing conversion of ‘((MaxFlowGraph<int>*)this)->MaxFlowGraph<int>::edg.std::vector<MaxFlowGraph<int>::edge, std::allocator<MaxFlowGraph<int>::edge> >::size()’ from ‘std::vector<MaxFlowGraph<int>::edge, std::allocator<MaxFlowGraph<int>::edge> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
14 | G[a].push_back({b, G[b].size(), edg.size(), c});
| ~~~~~~~~^~
main.cpp:15:39: warning: narrowing conversion of ‘((&((MaxFlowGraph<int>*)this)->MaxFlowGraph<int>::G.std::vector<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >, std::allocator<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> > > >::operator[](((std::vector<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >, std::allocator<std::vector<MaxFlowGraph<int>::edge_, std::allocator<MaxFlowGraph<int>::edge_> >
ソースコード
#include<bits/stdc++.h>
using namespace std;
template<class Cap> struct MaxFlowGraph{
struct edge{
int from, to;
Cap cap, flow;
};
MaxFlowGraph(int n): G(n), _n(n), cap_max(0){}
int add_edge(int a, int b, Cap c){
assert(0<=a && a<_n);
assert(0<=b && b<_n);
assert(0<=c);
G[a].push_back({b, G[b].size(), edg.size(), c});
G[b].push_back({a, G[a].size()-1, ~(int)edg.size(), 0});
edg.push_back({a, b, c, 0});
if(cap_max < c) cap_max = c;
return edg.size()-1;
}
Cap flow(int s, int t){
Cap res = 0;
while(true){
used.assign(_n, false);
Cap fw = dfs(s, t, cap_max);
if(fw==0)break;
res += fw;
}
return res;
}
std::vector<edge> edges(){return edg;}
edge get_edge(int k){return edg[k];}
private:
struct edge_{
int to, rev, id;
Cap cap;
};
int _n;
Cap cap_max;
std::vector<std::vector<edge_>> G;
std::vector<edge> edg;
std::vector<bool> used;
Cap dfs(int v, int t, Cap f){
if(v==t) return f;
used[v] = true;
for(edge_ &e:G[v]){
if(used[e.to] || e.cap==0) continue;
Cap fw = dfs(e.to, t, std::min<Cap>(f, e.cap));
if(fw==0) continue;
e.cap -= fw;
G[e.to][e.rev].cap += fw;
if(e.id<0) edg[~e.id].flow -= fw;
else edg[e.id].flow += fw;
return fw;
}
return 0;
}
};
constexpr int INF=1e9;
int main(){
int N; cin>>N;
int s=2*N,t=s+1;
MaxFlowGraph<int> G(2*N+2);
int sum=0;
for(int i=0;i<N;i++){
int B,C; cin>>B>>C;
sum+=B+C;
G.add_edge(s,i,B);
G.add_edge(N+i,t,C);
}
for(int i=0;i<N;i++)G.add_edge(i,N+i,INF);
int M; cin>>M;
for(int i=0;i<M;i++){
int a,b; cin>>a>>b;
G.add_edge(a,N+b,INF);
}
cout<<sum-G.flow(s,t)<<endl;
}