【hdu 3339】 In Action (Dijkstra+01揹包)

In Action

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3
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
 

#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
#define MAX 105
int map[MAX][MAX];
int visit[MAX]; 
int dis[MAX],pow[MAX],dp[MAX*MAX];
int n,m;
inline int max(int a,int b){return a>b?a:b;}
void Dijkstra()
{
     int i,j;
     for(i=1;i<=n;i++) dis[i]=map[0][i],visit[i]=false;
     visit[0]=true;
     dis[0]=0;
     
     for(i=1;i<=n;i++)
     {
                      int k,tmp=INF;
                      for(j=1;j<=n;j++)if(!visit[j]&&dis[j]<tmp) tmp=dis[k=j];
                      if(tmp==INF) break;
                      visit[k]=true;
                      for(j=1;j<=n;j++)
                        if(!visit[j]&&dis[j]>dis[k]+map[k][j]) 
                            dis[j]=dis[k]+map[k][j];
     }
}
int main()
{
    int T,i,j,st,ed,w;
    scanf("%d",&T);
    while(T--)
    {
         scanf("%d%d",&n,&m);
         for(i=0;i<=n;i++)
           for(j=0;j<=n;j++)
               map[i][j]=INF;
         for(i=1;i<=m;i++)
         {
             scanf("%d%d%d",&st,&ed,&w);    
             if(w<map[st][ed])
                  map[st][ed]=map[ed][st]=w;
         }
         Dijkstra();         
         //迪傑斯特拉後,dis[i]即爲每件物品的花費
         int sum=0,f=0; 
         for(i=1;i<=n;i++)
         {
             scanf("%d",&pow[i]);
             f+=pow[i];
             if(dis[i]!=INF) sum+=dis[i];
         } 
         memset(dp,0,sizeof(dp));
         dp[0]=0;            
         for(i=1;i<=n;i++)
           for(j=sum;j>=dis[i];j--)
               dp[j]=max(dp[j],dp[j-dis[i]]+pow[i]);
         f=f/2+1;
         int flag=0;
         for(i=0;i<sum;i++)         
           if(dp[i]>=f){ flag=1;break;}

         if(flag)  printf("%d\n",i); 
         else  printf("impossible\n");
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章