码迷,mamicode.com
首页 > 其他好文 > 详细

Hdoj 3339

时间:2018-02-21 23:37:22      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:scanf   single   problem   while   log   control   blog   cas   possible   

原题链接

描述

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.

输入

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.

输出

The minimal oil cost in this action.
If not exist print "impossible"(without quotes).

样例输入

2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3

样例输出

5
impossible

思路

最短路径+01背包问题
具体思路以后再写

代码

#include <bits/stdc++.h>
#define INF 99999999
#define maxn 102
using namespace std;

int dist[maxn][maxn];
int f[maxn];
int power[maxn], length[maxn], dp[100*maxn];
int m, n;

void init()  
{
    memset(f, 0, sizeof(f));
    memset(dp, 0, sizeof(dp));
    memset(power, 0, sizeof(power));
    for(int i = 0; i < maxn; i++)
        for(int j = 0; j < maxn; j++)
            dist[i][j] = INF;
}

void dijkstra(int v)
{
    for(int i = 0; i <= n; i++)
        length[i] = dist[v][i];
    f[v] = 1;
    while(1)
    {
        int min = INF, k = -1;
        for(int i = 1; i <= n; i++)
            if(!f[i] && min > length[i]) {min = length[i]; k = i;}
        if(k == -1) break;
        f[k] = 1;
        for(int i = 1; i <= n; i++)
            if(!f[i] && length[i] > min + dist[k][i])
                length[i] = min + dist[k][i];
    }
}

int main()
{
    int t; scanf("%d", &t);
    while(t--)
    {
        init();
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; i++)
        {
            int x, y, d;
            scanf("%d%d%d", &x, &y, &d);
            if(dist[x][y] > d) dist[x][y] = dist[y][x] = d;
        }
        dijkstra(0);
        int sum = 0, tot = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &power[i]);
            sum += power[i];
            if(length[i] < INF) tot += length[i];
        }
        for(int i = 1; i <= n; i++)
            if(length[i] < INF)
                for(int j = tot; j >= length[i]; j--)
                    dp[j] = max(dp[j], dp[j-length[i]] + power[i]);
        int f = 1, i;
        for(i = 1; i <= tot; i++)
            if(dp[i] * 2 > sum)
            {
                f = 0; break;
            }
        if(f) printf("impossible\n");
        else printf("%d\n", i);
    }
    return 0;  
}

Hdoj 3339

标签:scanf   single   problem   while   log   control   blog   cas   possible   

原文地址:https://www.cnblogs.com/HackHarry/p/8437234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!