답안 #888064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
888064 2023-12-15T21:33:54 Z Macker Training (IOI07_training) C++17
30 / 100
4 ms 856 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

int main()
{
    int n, m; cin >> n >> m;
    vector<vector<pair<int, int>>> adj(n);
    vector<vector<pair<int, int>>> v(n);
    vector<int> idx(n, -1);
    int res = 0;

    for (int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c; a--; b--;
        adj[a].push_back({c, b});
        adj[b].push_back({c, a});
    }
    int s;
    for (int i = 0; i < n; i++)
    {
        sort(all(adj[i]));
        if(adj[i].size() == 1 || adj[i][1].first != 0) s = i;
    }
    int par = s;
    for (int i = 0; i < n; i++)
    {
        idx[s] = i;
        for (auto &&j : adj[s]) {
            if(j.first != 0 && idx[j.second] != -1){
                if((i - idx[j.second]) % 2 == 1)
                    res += j.first;
                else
                    v[i].push_back({j.first, idx[j.second]});
            }
        }
        
        if(par != adj[s][0].second){
            par = s;
            s = adj[s][0].second;
        }
        else{
            par = s;
            s = adj[s][1].second;
        }
    }
    vector<int> dp(n);
    dp[0] = res;
    for (int i = 1; i < n; i++) {
        int sm = 0;
        for (int j = 0; j < v[i].size(); j++) {
            sm += v[i][j].first;
        }
        dp[i] = dp[i - 1] + sm;
        for (int j = 0; j < v[i].size(); j++) {
            int cur;
            if(v[i][j].second == 0) cur = sm - v[i][j].first + res;
            else cur = sm - v[i][j].first + dp[v[i][j].second];
            for (int k = v[i][j].second + 1; k < i; k++) {
                for (auto &&l : v[k])
                    cur += l.first;
            }
            dp[i] = min(dp[i], cur);
        }
    }
    cout << dp[n - 1] << endl;
}

Compilation message

training.cpp: In function 'int main()':
training.cpp:52:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |         for (int j = 0; j < v[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
training.cpp:56:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for (int j = 0; j < v[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
training.cpp:29:14: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
   29 |         idx[s] = i;
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 856 KB Output isn't correct
2 Halted 0 ms 0 KB -