hdu 3339 In Action dijkstra邻接矩阵+01背包

来源:互联网 发布:幻灯js代码 编辑:程序博客网 时间:2024/06/11 15:37

In Action

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


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
22 30 2 92 1 31 0 2132 12 1 313
 

Sample Output
5impossible
 

Author
Lost@HDU
 

Source
HDOJ Monthly Contest – 2010.03.06
 

Recommend
lcy
 

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;const int INF=0xfffff;const int maxn=111;int n,m;int a[maxn],f[maxn*maxn];int map[maxn][maxn],vis[maxn],dis[maxn];void inint(){    memset(a,0,sizeof(a));    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)            map[i][j]=INF;        map[i][i]=0;    }}void dijkstra(int s){    int i,j,k,mn;    for(i=1;i<=n;i++)      dis[i]=map[s][i],vis[i]=0;    vis[s]=1;    for(i=1;i<=n;i++)    {        k=s;        mn=INF;        for(j=1;j<=n;j++)            if(!vis[j]&&dis[j]<mn)                k=j,mn=dis[j];        vis[k]=1;        for(j=1;j<=n;j++)            if(!vis[j]&&(map[k][j]+dis[k])<dis[j])                dis[j]=dis[k]+map[k][j];    }}int main(){    int t;    cin>>t;    while(t--)    {        int i,j,res,sum;        int e,b,d;        scanf("%d%d",&n,&m);        n++;        inint();        for(i=0;i<m;i++)        {            scanf("%d%d%d",&e,&b,&d);            e++;b++;            if(map[e][b]>d)            map[e][b]=map[b][e]=d;        }        dijkstra(1);        sum=0;        for(i=2;i<=n;i++)          {              //cout<<dis[i]<<endl;              sum+=dis[i];          }          //cout<<sum<<endl;        res=0;        for(i=2;i<=n;i++)        {            scanf("%d",&a[i]);            res+=a[i];        }        if(dis[2]==INF)        {            cout<<"impossible\n";            continue;        }        memset(f,0,sizeof(f));        for(i=2;i<=n;i++)            for(j=sum;j>=dis[i];j--)              f[j]=max(f[j],f[j-dis[i]]+a[i]);        res=(res/2)+1;        //cout<<res<<endl;        for(i=1;i<=sum;i++)        {           // cout<<i<<" "<<f[i]<<endl;            if(f[i]>=res)            {                cout<<i<<endl;                break;            }        }    }    return 0;}