Submission #132198


Source Code Expand

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
//#include <gmpxx.h> 
#include <iostream>
//#include <map>
#include <queue>
#include <unordered_map>
#include <utility>
#include <set>
#include <sstream>
#include <stack>
#include <string>
//#include <sys/time.h>
#include <vector>
using namespace std;
#define li          int64_t
#define rep(i, n)   for(li i = 0; i < ((li)(n)); ++i)
#define pb          push_back
#define sz(v)       ((li)((v).size()))
#define bit(n)      (((li)1)<<((li)(n)))
#define all(v)      (v).begin(), (v).end()
#define each(it, v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define MP          make_pair
#define F           first
#define S           second



template <class T> class MinCostFlow
{
public:
  void addEdge(int from, int dest, int capacity, T cost);
  void terminateNegativeLoop(); // This requires all negative loops to be touched.
  T minCostFlow(int flow_source, int flow_target, int flow);

  MinCostFlow(int vertex_num);
  MinCostFlow(){}

private:
  class Edge
  {
  public:
    int dest;
    int capacity;
    int reversed_index;
    T cost;
    
    Edge(int dest, int capacity, int reversed_index, T cost)
    {
      this->dest = dest;
      this->capacity = capacity;
      this->reversed_index = reversed_index;
      this->cost = cost;
    }
  };
  T negative_loop_cost;
  int vertex_num;
  std::vector<std::vector<Edge>> edges;
};

template <class T>
inline MinCostFlow<T>::MinCostFlow(int vertex_num)
{
  this->vertex_num = vertex_num;
  negative_loop_cost = 0;
  edges = std::vector<std::vector<Edge>>(vertex_num);
}

template <class T>
inline void MinCostFlow<T>::addEdge(int from, int dest, int capacity, T cost)
{
  Edge e0(dest, capacity, edges[dest].size(),  cost);
  Edge e1(from,        0, edges[from].size(), -cost);
  edges[from].push_back(e0);
  edges[dest].push_back(e1);
}

template <class T>
inline T MinCostFlow<T>::minCostFlow(int flow_source, int flow_target, int flow)
{
  std::vector<std::tuple<T, bool>> costs(vertex_num);
  T total = 0;
  std::vector<int> pre_v(vertex_num);
  std::vector<int> pre_e(vertex_num);
  while(0 < flow){
    for(int vertex = 0; vertex < vertex_num; ++vertex) costs[vertex] = std::make_tuple(0, false);
    costs[flow_source] = std::make_tuple(0, true);
    for(bool updated = true; updated;){
      updated = false;
      for(int vertex = 0; vertex < vertex_num; ++vertex){
        if(!std::get<1>(costs[vertex])) continue;
        for(int edge_index = 0; edge_index < edges[vertex].size(); ++edge_index){
          Edge &e = edges[vertex][edge_index];
          if(e.capacity == 0) continue;
          if(std::get<1>(costs[e.dest]) && std::get<0>(costs[e.dest]) <= std::get<0>(costs[vertex]) + e.cost) continue;
          costs[e.dest] = std::make_tuple(std::get<0>(costs[vertex]) + e.cost, true);
          pre_v[e.dest] = vertex;
          pre_e[e.dest] = edge_index;
          updated = true;
        }
      }
    }
    if(!std::get<1>(costs[flow_target])) return -1;
    int minimum_flow = flow;
    for(int vertex = flow_target; vertex != flow_source; vertex = pre_v[vertex]){
      minimum_flow = std::min(minimum_flow, edges[pre_v[vertex]][pre_e[vertex]].capacity);
    }
    flow -= minimum_flow;
    
    total += minimum_flow * std::get<0>(costs[flow_target]);
    for(int vertex = flow_target; vertex != flow_source; vertex = pre_v[vertex]){
      Edge &e = edges[pre_v[vertex]][pre_e[vertex]];
      e.capacity -= minimum_flow;
      edges[vertex][e.reversed_index].capacity += minimum_flow;
    }
  }
  return total + negative_loop_cost;
}

template <class T> 
void MinCostFlow<T>::terminateNegativeLoop()
{
  std::vector<int> pre_v(vertex_num);
  std::vector<int> pre_e(vertex_num);
  std::vector<int> used(vertex_num);
  std::vector<T> distances(vertex_num);

  while(true){
    int vertex_in_loop = -1;
    for(int i = 0; i < vertex_num; i++) used[i] = distances[i] = 0;
    for(int step = 0; step <= vertex_num; ++step){
      bool updated=false;
      for(int vertex = 0; vertex < vertex_num; ++vertex){
        for(int edge_index = 0; edge_index < edges[vertex].size(); ++edge_index){
          Edge &e = edges[vertex][edge_index];
          if(e.capacity == 0 || distances[e.dest] <= distances[vertex] + e.cost) continue;
          distances[e.dest] = distances[vertex] + e.cost;
          pre_v[e.dest] = vertex;
          pre_e[e.dest] = edge_index;
          vertex_in_loop = e.dest;
          updated = true;
        }
      }
      if(!updated) return;
    }

    for(; !used[vertex_in_loop]; vertex_in_loop = pre_v[vertex_in_loop]) used[vertex_in_loop] = true;
    
    T total_cost = 0;
    int minimum_capacity = edges[pre_v[vertex_in_loop]][pre_e[vertex_in_loop]].capacity;
    int cur = vertex_in_loop;
    do{
      total_cost += edges[pre_v[cur]][pre_e[cur]].cost;
      minimum_capacity = std::min(minimum_capacity, edges[pre_v[cur]][pre_e[cur]].capacity);
      cur = pre_v[cur];
    }while(cur != vertex_in_loop);

    negative_loop_cost += total_cost * minimum_capacity;
    
    cur = vertex_in_loop;
    do{
      Edge &e = edges[pre_v[cur]][pre_e[cur]];
      e.capacity -= minimum_capacity;
      edges[cur][e.reversed_index].capacity += minimum_capacity;
      cur = pre_v[cur];
    }while(cur != vertex_in_loop);
  }
}





int main()
{
  li pos[3] = {-100, 0, 100};
  li num[3];
  cin >> num[0] >> num[1] >> num[2];
  
  const li GETA = 300 * 3;
  const li MAX = GETA * 2;
  MinCostFlow<li> flow(MAX + 5);
  li source = MAX + 0;
  li target = MAX + 1;
  li boxes[3] = {MAX + 2, MAX + 3, MAX + 4};
  
  rep(i, MAX)rep(j, 3) flow.addEdge(boxes[j], i, 1, abs(pos[j] - (i - GETA)));
  rep(i, 3) flow.addEdge(source, boxes[i], num[i], 0);
  rep(i, MAX) flow.addEdge(i, target, 1, 0);
  
  cout << flow.minCostFlow(source, target, num[0] + num[1] + num[2]) << endl;
}













Submission Info

Submission Time
Task D - マーブル
User Komaki
Language C++11 (GCC 4.8.1)
Score 100
Code Size 6120 Byte
Status AC
Exec Time 139 ms
Memory 1384 KB

Judge Result

Set Name sub1 sub2 All
Score / Max Score 10 / 10 30 / 30 60 / 60
Status
AC × 29
AC × 57
AC × 82
Set Name Test Cases
sub1 sample_01_ABC.txt, test_ABC_01.txt, test_ABC_02.txt, test_ABC_03.txt, test_ABC_04.txt, test_ABC_05.txt, test_ABC_06.txt, test_ABC_07.txt, test_ABC_08.txt, test_ABC_09.txt, test_ABC_10.txt, test_ABC_11.txt, test_ABC_12.txt, test_ABC_13.txt, test_ABC_14.txt, test_ABC_15.txt, test_ABC_16.txt, test_ABC_17.txt, test_ABC_18.txt, test_ABC_19.txt, test_ABC_20.txt, test_ABC_21.txt, test_ABC_22.txt, test_ABC_23.txt, test_ABC_24.txt, test_ABC_25.txt, test_ABC_26.txt, test_ABC_27.txt, test_ABC_28.txt
sub2 sample_01_ABC.txt, sample_02_BC.txt, test_ABC_01.txt, test_ABC_02.txt, test_ABC_03.txt, test_ABC_04.txt, test_ABC_05.txt, test_ABC_06.txt, test_ABC_07.txt, test_ABC_08.txt, test_ABC_09.txt, test_ABC_10.txt, test_ABC_11.txt, test_ABC_12.txt, test_ABC_13.txt, test_ABC_14.txt, test_ABC_15.txt, test_ABC_16.txt, test_ABC_17.txt, test_ABC_18.txt, test_ABC_19.txt, test_ABC_20.txt, test_ABC_21.txt, test_ABC_22.txt, test_ABC_23.txt, test_ABC_24.txt, test_ABC_25.txt, test_ABC_26.txt, test_ABC_27.txt, test_ABC_28.txt, test_BC_29.txt, test_BC_30.txt, test_BC_31.txt, test_BC_32.txt, test_BC_33.txt, test_BC_34.txt, test_BC_35.txt, test_BC_36.txt, test_BC_37.txt, test_BC_38.txt, test_BC_39.txt, test_BC_40.txt, test_BC_41.txt, test_BC_42.txt, test_BC_43.txt, test_BC_44.txt, test_BC_45.txt, test_BC_46.txt, test_BC_47.txt, test_BC_48.txt, test_BC_49.txt, test_BC_50.txt, test_BC_51.txt, test_BC_52.txt, test_BC_53.txt, test_BC_54.txt, test_BC_55.txt
All test_ABC_01.txt, test_ABC_02.txt, test_ABC_03.txt, test_ABC_04.txt, test_ABC_05.txt, test_ABC_06.txt, test_ABC_07.txt, test_ABC_08.txt, test_ABC_09.txt, test_ABC_10.txt, test_ABC_11.txt, test_ABC_12.txt, test_ABC_13.txt, test_ABC_14.txt, test_ABC_15.txt, test_ABC_16.txt, test_ABC_17.txt, test_ABC_18.txt, test_ABC_19.txt, test_ABC_20.txt, test_ABC_21.txt, test_ABC_22.txt, test_ABC_23.txt, test_ABC_24.txt, test_ABC_25.txt, test_ABC_26.txt, test_ABC_27.txt, test_ABC_28.txt, test_BC_29.txt, test_BC_30.txt, test_BC_31.txt, test_BC_32.txt, test_BC_33.txt, test_BC_34.txt, test_BC_35.txt, test_BC_36.txt, test_BC_37.txt, test_BC_38.txt, test_BC_39.txt, test_BC_40.txt, test_BC_41.txt, test_BC_42.txt, test_BC_43.txt, test_BC_44.txt, test_BC_45.txt, test_BC_46.txt, test_BC_47.txt, test_BC_48.txt, test_BC_49.txt, test_BC_50.txt, test_BC_51.txt, test_BC_52.txt, test_BC_53.txt, test_BC_54.txt, test_BC_55.txt, test_C_56.txt, test_C_57.txt, test_C_58.txt, test_C_59.txt, test_C_60.txt, test_C_61.txt, test_C_62.txt, test_C_63.txt, test_C_64.txt, test_C_65.txt, test_C_66.txt, test_C_67.txt, test_C_68.txt, test_C_69.txt, test_C_70.txt, test_C_71.txt, test_C_72.txt, test_C_73.txt, test_C_74.txt, test_C_75.txt, test_C_76.txt, test_C_77.txt, test_C_78.txt, test_C_79.txt, test_C_80.txt, test_C_81.txt, test_C_82.txt
Case Name Status Exec Time Memory
sample_01_ABC.txt AC 26 ms 1188 KB
sample_02_BC.txt AC 32 ms 1300 KB
sample_03_C.txt AC 119 ms 1308 KB
test_ABC_01.txt AC 23 ms 1300 KB
test_ABC_02.txt AC 25 ms 1196 KB
test_ABC_03.txt AC 23 ms 1312 KB
test_ABC_04.txt AC 25 ms 1308 KB
test_ABC_05.txt AC 24 ms 1192 KB
test_ABC_06.txt AC 24 ms 1308 KB
test_ABC_07.txt AC 23 ms 1308 KB
test_ABC_08.txt AC 25 ms 1304 KB
test_ABC_09.txt AC 24 ms 1312 KB
test_ABC_10.txt AC 25 ms 1304 KB
test_ABC_11.txt AC 27 ms 1308 KB
test_ABC_12.txt AC 27 ms 1304 KB
test_ABC_13.txt AC 28 ms 1308 KB
test_ABC_14.txt AC 25 ms 1308 KB
test_ABC_15.txt AC 23 ms 1304 KB
test_ABC_16.txt AC 28 ms 1312 KB
test_ABC_17.txt AC 25 ms 1204 KB
test_ABC_18.txt AC 25 ms 1300 KB
test_ABC_19.txt AC 26 ms 1312 KB
test_ABC_20.txt AC 25 ms 1312 KB
test_ABC_21.txt AC 24 ms 1184 KB
test_ABC_22.txt AC 24 ms 1304 KB
test_ABC_23.txt AC 30 ms 1348 KB
test_ABC_24.txt AC 29 ms 1384 KB
test_ABC_25.txt AC 27 ms 1304 KB
test_ABC_26.txt AC 28 ms 1308 KB
test_ABC_27.txt AC 24 ms 1304 KB
test_ABC_28.txt AC 38 ms 1312 KB
test_BC_29.txt AC 30 ms 1192 KB
test_BC_30.txt AC 32 ms 1364 KB
test_BC_31.txt AC 34 ms 1176 KB
test_BC_32.txt AC 33 ms 1300 KB
test_BC_33.txt AC 35 ms 1304 KB
test_BC_34.txt AC 36 ms 1308 KB
test_BC_35.txt AC 29 ms 1308 KB
test_BC_36.txt AC 34 ms 1200 KB
test_BC_37.txt AC 30 ms 1300 KB
test_BC_38.txt AC 34 ms 1200 KB
test_BC_39.txt AC 32 ms 1308 KB
test_BC_40.txt AC 35 ms 1304 KB
test_BC_41.txt AC 38 ms 1368 KB
test_BC_42.txt AC 37 ms 1308 KB
test_BC_43.txt AC 31 ms 1304 KB
test_BC_44.txt AC 30 ms 1304 KB
test_BC_45.txt AC 30 ms 1308 KB
test_BC_46.txt AC 36 ms 1348 KB
test_BC_47.txt AC 30 ms 1304 KB
test_BC_48.txt AC 38 ms 1200 KB
test_BC_49.txt AC 37 ms 1196 KB
test_BC_50.txt AC 37 ms 1308 KB
test_BC_51.txt AC 37 ms 1300 KB
test_BC_52.txt AC 29 ms 1312 KB
test_BC_53.txt AC 29 ms 1304 KB
test_BC_54.txt AC 28 ms 1192 KB
test_BC_55.txt AC 39 ms 1312 KB
test_C_56.txt AC 73 ms 1308 KB
test_C_57.txt AC 94 ms 1304 KB
test_C_58.txt AC 96 ms 1204 KB
test_C_59.txt AC 93 ms 1200 KB
test_C_60.txt AC 63 ms 1304 KB
test_C_61.txt AC 87 ms 1312 KB
test_C_62.txt AC 80 ms 1308 KB
test_C_63.txt AC 80 ms 1188 KB
test_C_64.txt AC 70 ms 1308 KB
test_C_65.txt AC 87 ms 1196 KB
test_C_66.txt AC 87 ms 1308 KB
test_C_67.txt AC 97 ms 1192 KB
test_C_68.txt AC 69 ms 1312 KB
test_C_69.txt AC 71 ms 1200 KB
test_C_70.txt AC 83 ms 1196 KB
test_C_71.txt AC 95 ms 1308 KB
test_C_72.txt AC 112 ms 1232 KB
test_C_73.txt AC 99 ms 1200 KB
test_C_74.txt AC 100 ms 1368 KB
test_C_75.txt AC 84 ms 1308 KB
test_C_76.txt AC 108 ms 1304 KB
test_C_77.txt AC 111 ms 1192 KB
test_C_78.txt AC 107 ms 1196 KB
test_C_79.txt AC 69 ms 1192 KB
test_C_80.txt AC 66 ms 1312 KB
test_C_81.txt AC 71 ms 1308 KB
test_C_82.txt AC 139 ms 1304 KB