zoukankan      html  css  js  c++  java
  • B. Hierarchy

    http://codeforces.com/problemset/problem/17/B

    用邻接矩阵建图后,

    设cost[v]表示去到顶点v的最小值。

    很多个人去顶点v的话,就选最小的那个就OK

    然后,如果有大于等于2个人的cost[v]是inf的,就不符合boss只有一个这个规矩。-1

    不应该只统计有孤立点就输出-1,因为m可以等于0(坑)

    另外这个图是不会有环的,因为有环就表明相对大小乱了。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    const int maxn = 1e3 + 20;
    int e[maxn][maxn];
    int cost[maxn];
    struct node {
        int u, v, w;
        int tonext;
    }E[10000 + 20];
    int first[maxn];
    int has[maxn];
    int num;
    void add(int u, int v, int w) {
        ++num;
        E[num].u = u;
        E[num].v = v;
        E[num].w = w;
        E[num].tonext = first[u];
        first[u] = num;
    }
    int a[maxn];
    void work() {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
        }
        int m;
        cin >> m;
        memset(e, 0x3f, sizeof e);
        for (int i = 1; i <= m; ++i) {
            int u, v, w;
            cin >> u >> v >> w;
            if (a[u] > a[v]) {
                has[v] = 1;
                has[u] = 1;
                e[u][v] = min(e[u][v], w);
            }
        }
        memset(cost, 0x3f, sizeof cost);
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (e[i][j] != inf) {
                    add(i, j, e[i][j]);
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = first[i]; j; j = E[j].tonext) {
                int v = E[j].v;
                cost[v] = min(cost[v], E[j].w);
            }
        }
        int ans = 0;
        int t = 0;
        for (int i = 1; i <= n; ++i) {
            if (cost[i] == inf) { //不能到达
                t++; //有一个没有老板了,
                if (t == 2) { //2个没有就不行了
                    cout << -1 << endl;
                    return;
                }
                continue;
            }
            ans += cost[i];
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt","r",stdin);
    #endif
        IOS;
        work();
        return 0;
    }
  • 相关阅读:
    融云使用
    cocoaPods使用
    电脑硬件
    拖图UI和纯代码UI
    7.2内存管理-ARC
    7内存管理-MRC
    数据刷新
    5.1音频播放
    2.6核心动画
    Git常用操作
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6032218.html
Copyright © 2011-2022 走看看