比赛 动规 评测结果 AAAAA
题目名称 医院设置 最终得分 100
用户昵称 kZime 运行时间 0.002 s
代码语言 C++ 内存使用 0.60 MiB
提交时间 2017-06-19 14:29:26
显示代码纯文本
# include <bits/stdc++.h>
using namespace std;

inline char getc() { 
    static char buf[1 << 18], *fs, *ft;
    return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
}

inline int gn() { 
    register int k = 0, f = 1;
    register char c = getc();
    for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
    for(; isdigit(c); c = getc()) k = k * 10 + c - '0';
    return k * f;
}

int n, a[102], f[102][102], ans = 0x7fffffff;

int main() { 
# ifndef LOCAL
    freopen("hospital.in", "r", stdin);
    freopen("hospital.out", "w", stdout);
# else 
    freopen("in", "r", stdin);
# endif
    n = gn();
    memset(f, 0x7f >> 2, sizeof(f));
    for(int l, r, i = 1; i <= n; i++) { 
        f[i][i] = 0;
        a[i] = gn();
        l = gn(), r = gn();
        if(l) f[i][l] = f[l][i] = 1;
        if(r) f[i][r] = f[r][i] = 1;
    }
    for(int k = 1; k <= n; k++) 
        for(int i = 1; i <= n; i++) 
            for(int j = 1; j <= n; j++) 
                f[i][j] = min(f[i][k] + f[k][j], f[i][j]);
    for(int i = 1; i <= n; i++) { 
        int sum = 0;
        for(int j = 1; j <= n; j++){ 
            if(i ^ j) { 
                sum += f[i][j] * a[j];
            }
        }
        ans = min(ans, sum);
    }
    printf("%d\n", ans);
}