Editorial for VOI 13 Bài 4 - Trộn xâu


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const long long oo = 1LL << 60;

int m, n, a[1010][1010];
long long f[1010][1010], g[1010][1010];

int main()
{
    int test;
    cin >> test;
    while (test--)
    {
        cin >> m >> n;
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", a[i] + j);

        for (int i = 0; i <= m; i++)
            for (int j = 0; j <= n; j++)
                f[i][j] = g[i][j] = oo;

        f[0][0] = g[0][0] = 0;

        for (int i = 0; i <= m; i++)
            for (int j = 0; j <= n; j++)
            {
                if (i < m)
                {
                    f[i + 1][j] = min(f[i + 1][j], f[i][j]);
                    f[i + 1][j] = min(f[i + 1][j], g[i][j] + a[i + 1][j]);
                }
                if (j < n)
                {
                    g[i][j + 1] = min(g[i][j + 1], g[i][j]);
                    g[i][j + 1] = min(g[i][j + 1], f[i][j] + a[i][j + 1]);
                }
            }

        cout << min(f[m][n], g[m][n]) << endl;
    }
}

Code mẫu của happyboy99x

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 1000
long long f[N+1][N+1][2]; int c[N+1][N+1], m, n;

void enter() {
    scanf("%d%d", &m, &n);
    for(int i = 1; i <= m; ++i)
        for(int j = 1; j <= n; ++j) {
            scanf("%d", &c[i][j]);
        }
}

void solve() {
    for(int j = 1; j <= n; ++j) f[0][j][1] = 0;
    for(int i = 1; i <= m; ++i) f[i][0][0] = 0; 
    for(int i = 1; i <= m; ++i)
        for(int j = 1; j <= n; ++j) {
            f[i][j][0] = min(f[i-1][j][0], f[i-1][j][1] + c[i][j]);
            f[i][j][1] = min(f[i][j-1][0] + c[i][j], f[i][j-1][1]);
        }
    printf("%lld\n", min(f[m][n][0], f[m][n][1]));
}

int main() {
    int tc; scanf("%d", &tc);
    memset(f, 0x7f, sizeof f);
    while(tc--) {
        enter();
        solve();
    }
    return 0;
}

Code mẫu của ladpro98

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <ctime>
#include <deque>
#include <bitset>
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, a, b) for(int i = (a); i <=(b); i++)
#define REPD(i, a, b) for(int i = (a); i >= (b); i--)
#define TR(i, a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); ++it)
#define RESET(a, v) memset((a), (v), sizeof((a)))
#define SZ(a) (int((a).size()))
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define II pair<int, int>
#define X first
#define Y second
#define LL long long
#define LD long double
#define VI vector<int>

const int N = 1010;
const LL oo = 100000000000009ll;

using namespace std;

void mini(LL &a, LL b) {a = a > b ? b : a;}
LL f[N][N][2]; int c[N][N];
int n, m, nTest;

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> nTest;
    while (nTest--) {
        cin >> m >> n;
        REP(i, 1, m) REP(j, 1, n) cin >> c[i][j];
        REP(i, 0, m) REP(j, 0, n) f[i][j][0] = f[i][j][1] = oo;
        f[0][0][0] = f[0][0][1] = 0;
        REP(i, 0, m) REP(j, 0, n) REP(k, 0, 1)
        if (f[i][j][k] < oo) {
            if (k) {
                mini(f[i + 1][j][0], f[i][j][k] + c[i + 1][j]);
                mini(f[i][j + 1][1], f[i][j][k]);
            }
            else {
                mini(f[i][j + 1][1], f[i][j][k] + c[i][j + 1]);
                mini(f[i + 1][j][0], f[i][j][k]);
            }
        }
        cout << min(f[m][n][0], f[m][n][1]) << '\n';
    }
}

Code mẫu của RR

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>

#define FOR(i,a,b) for(int i=(a),_b=(b); i <= _b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i >= _b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
using namespace std;

int c[1011][1011], f[1011][1011][2];
int m, n;

int main() {
    int ntest; scanf("%d", &ntest);
    while (ntest--) {
        scanf("%d%d", &m, &n);
        FOR(i,1,m) FOR(j,1,n) scanf("%d", &c[i][j]);

        FOR(i,0,m) FOR(j,0,n) REP(t,2) f[i][j][t] = 1000111000;

        f[0][0][0] = f[0][0][1] = 0;
        FOR(i,0,m) FOR(j,0,n) REP(t,2) {
            int cost;
            if (i < m) {
                if (t == 0) cost = 0; else cost = c[i+1][j];
                f[i+1][j][0] = min(f[i+1][j][0], f[i][j][t] + cost);
            }
            if (j < n) {
                if (t == 1) cost = 0; else cost = c[i][j+1];
                f[i][j+1][1] = min(f[i][j+1][1], f[i][j][t] + cost);
            }
        }
        cout << min(f[m][n][0], f[m][n][1]) << endl;
    }
    return 0;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
//#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
    if (rsz <= 0) {
        ptr = 0;
        rsz = (int) fread(bf, 1, bfsz, stdin);
        if (rsz <= 0)
            return EOF;
    }
    --rsz;
    return bf[ptr++];
}
void ga(char &c) {
    c = EOF;
    while (!isalpha(c))
        c = gc();
}
int gs(char s[]) {
    int l = 0;
    char c = gc();
    while (isspace(c))
        c = gc();
    while (c != EOF && !isspace(c)) {
        s[l++] = c;
        c = gc();
    }
    s[l] = '\0';
    return l;
}
template<class T> bool gi(T &v) {
    v = 0;
    char c = gc();
    while (c != EOF && c != '-' && !isdigit(c))
        c = gc();
    if (c == EOF)
        return false;
    bool neg = c == '-';
    if (neg)
        c = gc();
    while (isdigit(c)) {
        v = v * 10 + c - '0';
        c = gc();
    }
    if (neg)
        v = -v;
    return true;
}

typedef pair<int, int> II;

const ld PI = acos(-1.0);
const ld eps = 1e-9;

const int dr[] = {+2, +1, +2, +1, -2, -1, -2, -1};
const int dc[] = {+1, +2, -1, -2, +1, +2, -1, -2};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;

#define maxn 1005

int n, m, a[maxn][maxn];
ll f[maxn][maxn][2];
int test;

int  main()
{
//  freopen("in.txt", "r", stdin);

    gi(test);
    Rep(itest, test){
        gi(n); gi(m);
        For(i, 1, n) For(j, 1, m) gi(a[i][j]);

        ms(f, 0x3f);
        For(i, 1, n) f[i][0][0] = 0;
        For(i, 1, m) f[0][i][1] = 0;

        For(sum, 2, n + m){
            For(i, 1, min(n, sum - 1)){
                int j = sum - i;
                if(j > m) continue;
                f[i][j][0] = min(f[i][j][0], min(f[i - 1][j][0], f[i - 1][j][1] + a[i][j]));
                f[i][j][1] = min(f[i][j][1], min(f[i][j - 1][1], f[i][j - 1][0] + a[i][j]));
            }
        }

        cout << min(f[n][m][0], f[n][m][1]) << endl;

    }

    return 0;
}

Code mẫu của skyvn97

#include<stdio.h>
#define MAX   1111
typedef unsigned long long ull;
ull cost[MAX][MAX];
ull opt[MAX][MAX][5];
ull t,c;
ull m,n;
ull min(ull a,ull b)
{
    if (a<b) return (a); else return (b);
}
void init(void)
{
     ull i,j;
     scanf("%llu",&m);
     scanf("%llu",&n);
     for (i=1;i<=m;i=i+1)
         for (j=1;j<=n;j=j+1)
             scanf("%llu",&cost[i][j]);
}
void optimize(void)
{
     ull i,j;
     opt[1][1][1]=cost[1][1];
     opt[1][1][2]=cost[1][1];     
     for (i=2;i<=m;i=i+1)
         {
          opt[i][1][1]=min(opt[i-1][1][2]+cost[i][1],opt[i-1][1][1]);
          opt[i][1][2]=cost[i][1];
         }
     for (j=2;j<=n;j=j+1)
         {
          opt[1][j][1]=cost[1][j];
          opt[1][j][2]=min(opt[1][j-1][1]+cost[1][j],opt[1][j-1][2]);
         }
     for (i=2;i<=m;i=i+1)
         for (j=2;j<=n;j=j+1)
             {
              opt[i][j][1]=min(opt[i-1][j][1],opt[i-1][j][2]+cost[i][j]);
              opt[i][j][2]=min(opt[i][j-1][2],opt[i][j-1][1]+cost[i][j]);
             }             
     printf("%llu\n",min(opt[m][n][1],opt[m][n][2]));
}
int main(void)
{
    scanf("%llu",&t);
    for (c=1;c<=t;c=c+1)
        {
         init();
         optimize();
        }
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.