Submission #1212154

#TimeUsernameProblemLanguageResultExecution timeMemory
1212154dksnfjkfnwkfwMountains and Valleys (CCO20_day1problem3)C++20
3 / 25
602 ms115268 KiB
#include <bits/stdc++.h>
using namespace std;

#define sonic ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define IO(main) if(fopen(main".inp","r")){freopen(main".inp","r",stdin);freopen(main".out","w",stdout);}
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define ins insert
#define pb push_back
#define el cout << endl
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&(1LL))
#define FOR(i, a, b) for(int (i)=(a);(i)<=(b); i++)
#define FORD(i, a, b) for(int (i)=(a);(i)>=(b); i--)


using ll = long long;
using ull = unsigned long long;
using ld = long double;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;

const int N = 1e6 + 9;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int base = 31;
const int LOG = 20;
const ll MOD = 1e9 + 7;

int mul(int x, int y) {return 1LL * x * y % mod;}
int calPw(int x, int y)
{
    int ans = 1;
    while(y)
    {
        if (y&1) ans = 1LL * ans * x % mod;
        x = 1LL * x * x % mod;
        y >>= 1;
    }
    return ans;
}
int d4x[4] = {1, 0, -1, 0};
int d4y[4] = {0, 1, 0, -1};
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
///Author: Le Chi Thien Luong The Vinh high school 2008
///code
template<class X, class Y>
bool minimize(X &x , Y y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}
void add(int &x, int y){
    x+=y;
    if(x >= mod)x-=mod;
}
void sub(int &x, int y){
    x-=y;
    if(x < 0)x+=mod;
}
void Solve(){

}
int n, m;
int cost[500][500];
vector<pii>adj[N];
int dp[22][MASK(20) + 4];
void Read(){
    cin >> n >> m;
    FOR(i, 1, n){
        FOR(j, 1, n)cost[i][j] = INF;
    }
    FOR(i, 1, m){
        int u, v, w;
        cin >> u >> v >> w;
        u++;
        v++;
        cost[u][v] = min(cost[u][v], w);
        cost[v][u] = min(cost[v][u], w);
    }
    FOR(i, 1, n){
        FOR(j, 1, n){
            FOR(k, 1, n){
                if(cost[i][j] > cost[i][k] + cost[j][k]){
                    cost[i][j] = cost[i][k] + cost[j][k];
                }
            }
        }
    }
    FOR(i, 0, n)FOR(mask, 0, MASK(n) - 1){
        dp[i][mask] = INF;
    }
    FOR(i, 1, n)dp[i][MASK(i-1)] = 0;
    FOR(mask, 0, MASK(n) - 1){
        FOR(i, 0, n - 1){
            if(dp[i+1][mask] == INF)continue;
            if(BIT(mask, i)){
                FOR(j, 0, n - 1){
                    if(BIT(mask, j) == 0){
                        ///cout << i + 1 << " " << j + 1 << " " << cost[i+1][j+1] << endl;
                        dp[j + 1][mask | MASK(j)] = min(dp[j + 1][mask | MASK(j)], dp[i + 1][mask] + cost[i + 1][j + 1]);
                    }
                }
            }
        }
    }
    int ans = 1e9;
    FOR(i, 1, n){
     ans = min(ans, dp[i][MASK(n) - 1]);
    /// cout << dp[i][MASK(n) - 1] << endl;
    }
    cout << ans;
}



int main()
{
    sonic;

    int TEST =  1;
   // cin >> TEST;
    while(TEST--)
    {

        Read();
        Solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...