結果

問題 No.119 旅行のツアーの問題
ユーザー yaoshimaxyaoshimax
提出日時 2015-03-29 00:59:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,809 bytes
コンパイル時間 1,540 ms
コンパイル使用メモリ 88,204 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 09:16:01
合計ジャッジ時間 1,823 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#define SOURCE 0
#define SINK 250
using namespace std;
#define GRAPH_SIZE 251

struct edge{ int to, cap, rev; };
vector<edge> graph[GRAPH_SIZE];
bool used[GRAPH_SIZE];

void addedge( int from, int to, int cap ){
  //枝を追加する。逆向きには容量0の真逆な枝を追加しておく。
  graph[from].push_back( (edge){to,cap,(int)graph[to].size()} );
  graph[to].push_back( (edge){from,0, (int)graph[from].size()-1} );
  return;
}

int dfs( int v, int t, int f ){
  if( v==t ) return f;
  used[v]=true;
  for( int i = 0 ; i < (int)graph[v].size(); i++){
    edge &e = graph[v][i];
    if( !used[e.to] && e.cap > 0 ){
      int d = dfs(e.to, t, min(f,e.cap) );
      if( d > 0 ){
        e.cap -= d;
        graph[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow( int s, int t ){
  int flow = 0;
  while(true){
    memset(used, 0 , sizeof(used) );
    int f = dfs(s,t,INT_MAX/2);
    if( f == 0 ) return flow;
    flow += f;
  }
}


int main(){
   int N;
   cin >> N;
   int score[N+1][2];
   int tot=0;
   for( int i = 1 ; i <= N ; i++){
      cin >> score[i][0] >>score[i][1];
      tot+=score[i][0]+score[i][1];
      addedge(SOURCE,2*i,score[i][0]);
      addedge(2*i+1,SINK,score[i][1]);
      addedge(2*i,2*i+1,INT_MAX/2);
   }
   int M;
   cin >> M;
   for( int i=0;i<M;i++){
      int d,e;
      cin>>d>>e;
      addedge(2*d+2,2*e+3,INT_MAX/2);
   }
   cout << tot-max_flow(SOURCE,SINK);
   return 0;
}
0