zoukankan      html  css  js  c++  java
  • hdu In Action

    In Action

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 169    Accepted Submission(s): 78
     
    Problem Description
        Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across the globe.     Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weapons and wanna destroy our world. Fortunately, our mysterious spy-net has gotten his plan. Now, we need to stop it.     But the arduous task is obviously not easy. First of all, we know that the operating system of the nuclear weapon consists of some connected electric stations, which forms a huge and complex electric network. Every electric station has its power value. To start the nuclear weapon, it must cost half of the electric network\\\\\\\'s power. So first of all, we need to make more than half of the power diasbled. Our tanks are ready for our action in the base(ID is 0), and we must drive them on the road. As for a electric station, we control them if and only if our tanks stop there. 1 unit distance costs 1 unit oil. And we have enough tanks to use.     Now our commander wants to know the minimal oil cost in this action.
     
    Input
    The first line of the input contains a single integer T, specifying the number of testcase in the file.     For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the stations(the IDs are 1,2,3...n), and the number of the roads between the station(bi-direction).     Then m lines follow, each line is interger st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying the start point, end point, and the distance between.     Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric station\\\\\\\'s power by ID order.
     
    Output
                The minimal oil cost in this action.     If not exist print \\\\\\\"impossible\\\\\\\"(without quotes).
     
    Sample Input
    2
    2 3
    0 2 9
    2 1 3
    1 0 2
    1
    3
    2 1
    2 1 3
    1
    3
     
    Sample Output
    5
    impossible
     
    Author
    Lost@HDU
     
    Source
    HDOJ Monthly Contest – 2010.03.06
     
    Recommend
    lcy
    分析:先算出起始点到各个点的最短路,然后用背包得出超过一半总能量的最少站点数。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    using namespace std;
    #define maxn 0xfffffff
    int vis[110], d[110], p[110];
    int dp[10010];
    typedef pair<int, int> pii;
    
    typedef struct S {
        int v, w;
        struct S *next;
    } EDGE;
    EDGE *node[110], edge[20010];
    char op[2];
    int m, n, cnt;
    
    void add(int a, int b, int c) {
        edge[cnt].v = b;
        edge[cnt].w = c;
        edge[cnt].next = node[a];
        node[a] = &edge[cnt++];
    }
    
    int min(int a, int b) {
        return a < b ? a : b;
    }
    
    int main() {
        int i, j, a, b, c, t, ans;
        int T, sum;
        pii u;
        scanf("%d", &T);
        while (T--) {
            scanf("%d%d", &n, &m);
            memset(vis, 0, sizeof (vis));
            for (i = 0; i <= n; ++i) {
                d[i] = maxn;
                node[i] = NULL;
            }
            d[0] = 0;
            cnt = 0;
            sum = 0;
            while (m--) {
                scanf("%d%d%d", &a, &b, &c);
                add(a, b, c);
                add(b, a, c);
            }
            for (i = 1; i <= n; ++i) {
                scanf("%d", &p[i]);
                sum += p[i];
            }
            for (i = 1; i <= sum; ++i)
                dp[i] = maxn;
            dp[0]=0;
            priority_queue<pii, vector<pii>, greater<pii> > q;
            q.push(make_pair(d[0], 0));
            while (!q.empty()) {
                u = q.top();
                q.pop();
                a = u.second;
                vis[a] = 1;
                for (; node[a]; node[a] = node[a]->next) {
                    b = node[a]->v;
                    if (!vis[b] && d[a] + node[a]->w < d[b]) {
                        d[b] = d[a] + node[a]->w;
                        q.push(make_pair(d[b], b));
                    }
                }
            }
           
            for (i = 1; i <= n; ++i) {
                for (j = sum; j >= p[i]; --j) {
                    dp[j] = min(dp[j], dp[j - p[i]] + d[i]);
                }
            }
            ans = maxn;
            for (i = sum / 2 + 1; i <= sum; ++i) {
                if (dp[i] == maxn)
                    continue;
                if (dp[i] < ans)
                    ans = dp[i];
            }
            if (ans == maxn)
                printf("impossible\n");
            else
                printf("%d\n", ans);
        }
        return 0;
    }
    这条路我们走的太匆忙~拥抱着并不真实的欲望~
  • 相关阅读:
    CIA泄露资料分析(黑客工具&技术)—Windows篇
    包学会之浅入浅出Vue.js:结业篇
    包学会之浅入浅出Vue.js:升学篇
    包学会之浅入浅出Vue.js:开学篇
    Manacher算法详解
    CSP-S 2019 游记
    洛谷 P3373 【模板】线段树 2
    AHOI 2009 维护序列
    洛谷 P4017 最大食物链计数
    洛谷 SP14932 LCA
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2710053.html
Copyright © 2011-2022 走看看