In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5634    Accepted Submission(s): 1886


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
 

题目大意:

                   给你n个核电站编号为1-n和m条边(双向边)连接两个核电站,每个核电站有一个功率,现在在位置0有足够的坦克,问你最小的花费使得坦克占领的核电站额功率和大于总的功率的一半


题目思路:

                  首先花费最小可以跑个最短路(spfa或dij),然后就得出了从位置0到其他位置的最小花费,但是还有一个条件-功率超过一半,转换一下就是有n个物品,每个 物品有一个权值和一个花费,从中选出几个物品使得权值超过总的一半的最小花费,这就是01背包的模型,dp[i]表示权值为i时的最小花费,所以最后只需枚举大于一 半权值的最小话费,得出一个最小的!


AC代码:

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

const int maxn = 1e2+10;
const int inf = 1e9;

int n,m,e;
int hed[maxn],dis[maxn],vis[maxn],w[maxn],dp[maxn*maxn*2];

struct st{
   int v,w,nex;
}edge[maxn*maxn*2];

void add(int u,int v,int w){
   edge[e].v =v,edge[e].w = w,edge[e].nex = hed[u],hed[u]=e++;
}

void spfa(){
    for(int i=1;i<=n;i++)
        vis[i]=0,dis[i]=inf;
    vis[0]=1,dis[0]=0;
    queue<int>q;q.push(0);
    while(!q.empty()){
        int u = q.front();q.pop();
        vis[u]=0;
        for(int i = hed[u];~i;i=edge[i].nex){
            int v = edge[i].v;
            if(dis[v]>dis[u]+edge[i].w){
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }

}

int main()
{
    int t;cin>>t;
    while(t--){
        scanf("%d%d",&n,&m);
        memset(hed,-1,sizeof(hed)),e=0;
        for(int i=0;i<m;i++){
            int u,v,w;scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        int sum = 0;
        for(int i=1;i<=n;i++){scanf("%d",&w[i]);sum+=w[i];}
        spfa();
        int cnt=(sum+1)/2+(sum%2==0?1:0);
        for(int i=1;i<=sum;i++)
            dp[i]=inf;
        dp[0]=0;
        for(int i=1;i<=n;i++)
            for(int j=sum;j>=w[i];j--)
                dp[j]=min(dp[j],dp[j-w[i]]+dis[i]);   //01背包
                
        for(int i=cnt;i<=sum;i++)
            dp[cnt]=min(dp[cnt],dp[i]);
            
        if(dp[cnt]==inf)printf("impossible\n");
        else printf("%d\n",dp[cnt]);
    }

    return 0;
}