제출 #554517

#제출 시각아이디문제언어결과실행 시간메모리
554517Zhora_004도로 폐쇄 (APIO21_roads)C++17
컴파일 에러
0 ms0 KiB
#include "roads.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
#include <fstream>
//#include <bit>
//#include <ranges>
//#include <numbers>
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
/*ofstream fout("timeline.out");
ifstream fin("timeline.in");*/
using ll = long long;
using namespace std;
int n, k;
vector<int> a, b, c;
vector<vector<int>> tree;
vector<vector<ll>> dp;

bool task(int id)
{
	if (id == 1)
	{
		for (int& i : a) if (i) return 0;
		return 1;
	}
	if (id == 2)
	{
		for (int i = 0; i < n - 1; i++) if (a[i] != i || b[i] != i + 1) return 0;
		return 1;
	}
	return 0;
}

void dfs(int u, int p, int w)
{
	for (pair<int, int>& edge : tree[u]) if (edge.first != p) dfs(edge.first, u, edge.second);
	int can = k;
	dp[u][0] = 0;
	if (p != -1) can--;
	int cnt = sz(tree[u]);
	if (p != -1) cnt--;
	vector<vector<ll>> d(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf));
	d[0][0] = 0;
	int cur = 1;
	for (int i = 0; i < sz(tree[u]); i++)
	{
		int v = tree[u][i].first, len = tree[u][i].second;
		if (v == p) continue;
		for (int j = 0; j <= min(cur, can); j++)
		{
			if (cur - 1 >= j) d[cur][j] = d[cur - 1][j] + dp[v][1];
			if (j) d[cur][j] = min(d[cur][j], d[cur - 1][j - 1] + dp[v][0]);
		}
		cur++;
	}
	dp[u][0] = 1ll * inf * inf;
	for (int j = 0; j <= can; j++) dp[u][0] = min(dp[u][0], d[cur - 1][j]);

	if (p != -1)
	{
		can = k;
		d = vector<vector<ll>>(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf));
		d[0][0] = 0;
		cur = 1;
		for (int i = 0; i < sz(tree[u]); i++)
		{
			int v = tree[u][i].first, len = tree[u][i].second;
			if (v == p) continue;
			for (int j = 0; j <= min(cur, can); j++)
			{
				if (cur - 1 >= j) d[cur][j] = d[cur - 1][j] + dp[v][1];
				if (j) d[cur][j] = min(d[cur][j], d[cur - 1][j - 1] + dp[v][0]);
			}
			cur++;
		}
		dp[u][1] = 1ll * inf * inf;
		for (int j = 0; j <= can; j++) dp[u][1] = min(dp[u][1], d[cur - 1][j]);
		dp[u][1] += w;
	}
}

std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N;
	a = U;
	b = V;
	c = W;
	if (task(1))
	{
		vector<ll> res;
		ll sum = 0;
		for (int& i : c) sum += i;
		res.push_back(sum);
		sort(c.begin(), c.end());
		reverse(c.begin(), c.end());
		for (k = 0; k < n - 1; k++)
		{
			sum -= c[k];
			res.push_back(sum);
		}
		return res;
	}
	if (task(2))
	{
		ll sum = 0;
		dp = vector<vector<ll>>(n, vector<ll>(2));
		for (int i = 1; i < n; i++)
		{
			dp[i][0] = dp[i - 1][1];
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + c[i - 1];
			sum += c[i - 1];
		}
		vector<ll> res;
		res.push_back(sum);
		res.push_back(min(dp[n - 1][0], dp[n - 1][1]));
		for (k = 2; k < n; k++) res.push_back(0);
		return res;
	}
	else
	{
		ll tmp = 0;
		for (int i = 0; i < n - 1; i++)
		{
			int u, v, w;
			u = a[i], v = b[i], w = c[i];
			tree[u].push_back({ v, w });
			tree[v].push_back({ u, w });
			tmp += w;
		}
		vector<ll> res;
		res.push_back(tmp);
		for (k = 1; k < n; k++)
		{
			dp = vector<vector<ll>>(n, vector<ll>(2));
			dfs(0, -1, -1);
			res.push_back(dp[0][0]);
		}
		return res;
	}
}

컴파일 시 표준 에러 (stderr) 메시지

roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:58:36: error: invalid initialization of reference of type 'std::pair<int, int>&' from expression of type 'int'
   58 |  for (pair<int, int>& edge : tree[u]) if (edge.first != p) dfs(edge.first, u, edge.second);
      |                                    ^
roads.cpp:64:58: error: 'inf' was not declared in this scope; did you mean 'ynf'?
   64 |  vector<vector<ll>> d(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf));
      |                                                          ^~~
      |                                                          ynf
roads.cpp:69:22: error: request for member 'first' in '(& tree.std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)u)))->std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   69 |   int v = tree[u][i].first, len = tree[u][i].second;
      |                      ^~~~~
roads.cpp:89:23: error: request for member 'first' in '(& tree.std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)u)))->std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   89 |    int v = tree[u][i].first, len = tree[u][i].second;
      |                       ^~~~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:147:30: error: no matching function for call to 'std::vector<int>::push_back(<brace-enclosed initializer list>)'
  147 |    tree[u].push_back({ v, w });
      |                              ^
In file included from /usr/include/c++/10/vector:67,
                 from roads.h:1,
                 from roads.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1187:35: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const value_type&' {aka 'const int&'}
 1187 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1203:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::vector<int>::value_type&&' {aka 'int&&'}
 1203 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
roads.cpp:148:30: error: no matching function for call to 'std::vector<int>::push_back(<brace-enclosed initializer list>)'
  148 |    tree[v].push_back({ u, w });
      |                              ^
In file included from /usr/include/c++/10/vector:67,
                 from roads.h:1,
                 from roads.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1187:35: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const value_type&' {aka 'const int&'}
 1187 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1203:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::vector<int>::value_type&&' {aka 'int&&'}
 1203 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~