Submission #1294094


Source Code Expand

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
 
#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl
 
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
typedef deque<bool> db;
template<class T> using vv=vector<vector< T > >;
 
// cout vector
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size(); s << "\n";
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << "\t";
  }
  s << "\n"; return s;
}
 
// cout 2-dimentional vector
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) { s << vv[i]; }
  return s;
}
 
 
class UF {
private:
  vector<int> par; // parent
  vector<int> sizes;
  void init(int n) {
    par.resize(n);
    for (int i = 0; i < n; ++i) {
      par[i] = i;
    }
    sizes.assign(n, 1);
  }
 
  void clear() {
    par.clear();
    sizes.clear();
  }
 
public:
  UF() {}
  UF(int n) {
    init(n);
  }
 
  void assign(int n) {
    clear();
    init(n);
  }
 
  int root(int x) {
    if (x == par[x]) {
      return x;
    }
    return par[x] = root(par[x]);
  }
 
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (sizes[x] < sizes[y]) {
      swap(x, y);
    }
    par[y] = x;
    sizes[x] += sizes[y];
    sizes[y] = 0;
  }
 
  bool same(int x, int y) {
    return root(x) == root(y);
  }
 
  int size(int x) {
    return sizes[root(x)];
  }
};
 
int a, t, k;
vi n;
vector<set<int> > cities_including;
vector<set<int> > towns_included_by;
 
vector<vvi> edges_included_by;
int m;
 
int common_city(int town1, int town2) {
  if (cities_including[town1].size() > cities_including[town2].size()) {
    swap(town1, town2);
  }
  for (int city : cities_including[town1]) {
    if (cities_including[town2].find(city) != cities_including[town2].end()) {
      return city;
    }
  }
  assert(false);
  return -1;
}
 
int num_edges;
int num_towns;
vi costs;
unordered_map<int, int> town_id;
vvi g;
 
void dfs(int city, int edge_index, int used_edges, int cost_sum, UF uf) {
  if (edge_index < num_edges - 1) {
    dfs(city, edge_index + 1, used_edges, cost_sum, uf);
  }
  if (!(uf.same(g[edge_index][0], g[edge_index][1]))) {
    uf.unite(g[edge_index][0], g[edge_index][1]);
    cost_sum += g[edge_index][2];
    edge_index += 1;
    used_edges += 1;
    if (used_edges == num_towns - 1) {
      costs.push_back(cost_sum);
      return;
    }
    if (edge_index == num_edges) {
      return;
    }
    dfs(city, edge_index, used_edges, cost_sum, uf);
  }
}
 
void dfs_main(int city) {
  num_edges = (int)edges_included_by[city].size();
  num_towns = (int)towns_included_by[city].size();
  costs.clear();
//  costs.reserve(HOGE);
  UF uf;
  uf.assign(num_towns);
  town_id.clear();
  for (int town : towns_included_by[city]) {
    town_id[town] = (int)town_id.size();
  }
  g.clear();
  for (vi edge : edges_included_by[city]) {
    int u = town_id[edge[0]];
    int v = town_id[edge[1]];
    g.push_back({u, v, edge[2]});
  }
 
  dfs(city, 0, 0, 0, uf);
}
 
ll INF = 1e13;
 
int main() {
  scanf("%d%d%d", &a, &t, &k);
  n.resize(a);
  cities_including.resize(t);
  towns_included_by.resize(a);
  rep (i, a) { // city number
    scanf("%d", &n[i]);
    rep (j, n[i]) {
      int town;
      scanf("%d", &town);
      town -= 1;
      cities_including[town].insert(i);
      towns_included_by[i].insert(town);
    }
  }
 
  int sum_all_edges = 0;
  edges_included_by.resize(a);
  scanf("%d", &m);
  rep (i, m) {
    int town1, town2, cost;
    scanf("%d%d%d", &town1, &town2, &cost);
    town1 -= 1;
    town2 -= 1;
    int city = common_city(town1, town2);
    if (town1 > town2) { swap(town1, town2); }
    edges_included_by[city].push_back((vi){town1, town2, cost});
    sum_all_edges += cost;
  }
 
  vvi costs_of_city(a, vi(77*6+1, 0)); // Max(cost_i) * (7C2 - 6)
  ll judge = 1;
  rep (i, a) {
    int sum_costs = 0;
    for (auto e : edges_included_by[i]) {
      sum_costs += e[2];
    }
 
    dfs_main(i);
//    assert((int)costs.size() > 0);
    judge *= (int)costs.size(); // costs is the list of spanning trees
    judge = min(judge, INF);
    rep (j, (int)costs.size()) {
//      int cost = sum_costs - costs[j];
//      costs_of_city[i][cost] += 1;
      costs_of_city[i][costs[j]] += 1;
    }
  }
  if (judge < k) {
    printf("-1\n");
    return 0;
  }
 
  vll dp(77*6*77+1, 0); // Max(cost_i) * (7 - 1) * A
  dp[0] = 1;
  rep (i, a) {
    vll ndp(77*6*77+1, 0);
    rep (j, 77*6+1) {
      if (costs_of_city[i][j] == 0) {
        continue;
      }
      rep (ii, 77*6*77+1) {
        if (dp[ii] == 0) {
          continue;
        }
        ndp[j + ii] += min(INF, dp[ii]) * costs_of_city[i][j];
      }
    }
    swap(dp, ndp);
  }
 
  vector<pair<int, ll> > ans;
  ans.reserve((int)dp.size());
 
  rep (i, 77*6*77+1) {
    if (dp[i] == 0) {
      continue;
    }
    ans.push_back(make_pair(sum_all_edges - i, dp[i]));
  }
  sort(all(ans));
 
  ll k_ = k;
  for (auto p : ans) {
    k_ -= p.second;
    if (k_ <= 0) {
      printf("%d\n", p.first);
      return 0;
    }
  }
 
  return 0;
}

Submission Info

Submission Time
Task D - 覚醒ノ高橋君
User eukaryo
Language C++14 (Clang 3.8.0)
Score 100
Code Size 5999 Byte
Status AC
Exec Time 657 ms
Memory 1488 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 63
Set Name Test Cases
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_randt_00.txt, 01_randt_01.txt, 01_randt_02.txt, 01_randt_03.txt, 01_randt_04.txt, 01_randt_05.txt, 01_randt_06.txt, 01_randt_07.txt, 01_randt_08.txt, 01_randt_09.txt, 01_randt_10.txt, 01_randt_11.txt, 01_randt_12.txt, 01_randt_13.txt, 01_randt_14.txt, 01_randt_15.txt, 01_randt_16.txt, 01_randt_17.txt, 01_randt_18.txt, 01_randt_19.txt, 01_randt_20.txt, 01_randt_21.txt, 01_randt_22.txt, 01_randt_23.txt, 01_randt_24.txt, 01_randt_25.txt, 01_randt_26.txt, 01_randt_27.txt, 01_randt_28.txt, 01_randt_29.txt, 02_maxrandt_00.txt, 02_maxrandt_01.txt, 02_maxrandt_02.txt, 02_maxrandt_03.txt, 02_maxrandt_04.txt, 02_maxrandt_05.txt, 02_maxrandt_06.txt, 02_maxrandt_07.txt, 02_maxrandt_08.txt, 02_maxrandt_09.txt, 02_maxrandt_10.txt, 02_maxrandt_11.txt, 02_maxrandt_12.txt, 02_maxrandt_13.txt, 02_maxrandt_14.txt, 02_maxrandt_15.txt, 02_maxrandt_16.txt, 02_maxrandt_17.txt, 02_maxrandt_18.txt, 02_maxrandt_19.txt, 03_overflow_00.txt, 03_overflow_01.txt, 03_overflow_02.txt, 03_overflow_03.txt, 03_overflow_04.txt, 03_overflow_05.txt, 03_overflow_06.txt, 03_overflow_07.txt, 03_overflow_08.txt, 03_overflow_09.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 6 ms 1224 KB
00_sample_02.txt AC 1 ms 256 KB
00_sample_03.txt AC 2 ms 872 KB
01_randt_00.txt AC 60 ms 1024 KB
01_randt_01.txt AC 57 ms 1104 KB
01_randt_02.txt AC 14 ms 1000 KB
01_randt_03.txt AC 2 ms 872 KB
01_randt_04.txt AC 5 ms 976 KB
01_randt_05.txt AC 1 ms 256 KB
01_randt_06.txt AC 1 ms 256 KB
01_randt_07.txt AC 5 ms 896 KB
01_randt_08.txt AC 15 ms 896 KB
01_randt_09.txt AC 126 ms 1232 KB
01_randt_10.txt AC 160 ms 1232 KB
01_randt_11.txt AC 11 ms 896 KB
01_randt_12.txt AC 1 ms 256 KB
01_randt_13.txt AC 8 ms 896 KB
01_randt_14.txt AC 11 ms 896 KB
01_randt_15.txt AC 37 ms 1104 KB
01_randt_16.txt AC 39 ms 976 KB
01_randt_17.txt AC 1 ms 256 KB
01_randt_18.txt AC 1 ms 256 KB
01_randt_19.txt AC 8 ms 976 KB
01_randt_20.txt AC 1 ms 256 KB
01_randt_21.txt AC 6 ms 896 KB
01_randt_22.txt AC 22 ms 976 KB
01_randt_23.txt AC 1 ms 256 KB
01_randt_24.txt AC 4 ms 384 KB
01_randt_25.txt AC 9 ms 896 KB
01_randt_26.txt AC 27 ms 976 KB
01_randt_27.txt AC 116 ms 1232 KB
01_randt_28.txt AC 41 ms 976 KB
01_randt_29.txt AC 144 ms 1232 KB
02_maxrandt_00.txt AC 176 ms 1232 KB
02_maxrandt_01.txt AC 370 ms 1360 KB
02_maxrandt_02.txt AC 657 ms 1488 KB
02_maxrandt_03.txt AC 298 ms 1360 KB
02_maxrandt_04.txt AC 25 ms 1104 KB
02_maxrandt_05.txt AC 44 ms 1140 KB
02_maxrandt_06.txt AC 38 ms 1128 KB
02_maxrandt_07.txt AC 31 ms 1104 KB
02_maxrandt_08.txt AC 427 ms 1360 KB
02_maxrandt_09.txt AC 620 ms 1488 KB
02_maxrandt_10.txt AC 447 ms 1360 KB
02_maxrandt_11.txt AC 245 ms 1360 KB
02_maxrandt_12.txt AC 577 ms 1488 KB
02_maxrandt_13.txt AC 50 ms 1232 KB
02_maxrandt_14.txt AC 39 ms 1128 KB
02_maxrandt_15.txt AC 399 ms 1360 KB
02_maxrandt_16.txt AC 144 ms 1232 KB
02_maxrandt_17.txt AC 34 ms 1104 KB
02_maxrandt_18.txt AC 633 ms 1488 KB
02_maxrandt_19.txt AC 510 ms 1488 KB
03_overflow_00.txt AC 7 ms 1104 KB
03_overflow_01.txt AC 7 ms 1128 KB
03_overflow_02.txt AC 20 ms 1152 KB
03_overflow_03.txt AC 214 ms 1384 KB
03_overflow_04.txt AC 68 ms 1232 KB
03_overflow_05.txt AC 8 ms 1128 KB
03_overflow_06.txt AC 83 ms 1256 KB
03_overflow_07.txt AC 44 ms 1152 KB
03_overflow_08.txt AC 8 ms 1128 KB
03_overflow_09.txt AC 11 ms 1128 KB