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

In Action

时间:2014-09-15 15:57:19      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:spfa+01背包

Problem Description
[img]http://acm.hdu.edu.cn/data/images/C235-1007-1.jpg[/img] 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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 2000000000;
const int MX = 10010;
struct node
{
    int w,v,next;
}s[MX<<2];
int n,m,cnt;
int head[MX],dis[MX],p[10010],dp[10010];
void Init()
{
   cnt=0;
   memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    s[cnt].v=v;
    s[cnt].w=w;
    s[cnt].next=head[u];
    head[u]=cnt++;
}
void spfa(int ss)
{
    int i,v,w,tmp;
    bool vis[MX];
    queue<int>q;
    for(i=0;i<=n;i++)
    {
        dis[i]=INF;
        vis[i]=false;
    }
    dis[ss]=0;
    q.push(ss);
    while(!q.empty())
    {
       tmp = q.front();
       q.pop();
       vis[tmp]=false;
       for(i=head[tmp];i!=-1;i=s[i].next)
       {
           v = s[i].v;
           w = s[i].w;
           if(dis[v]>dis[tmp]+w)
           {
               dis[v]=dis[tmp]+w;
               if(!vis[v])
               {
                   vis[v]=true;
                   q.push(v);
               }
           }
       }
    }
}
int main()
{
  int x,y,z,t;
  scanf("%d",&t);
  while(t--)
  {
      scanf("%d%d",&n,&m);
      Init();
      for(int i=0;i<m;i++)
      {
          scanf("%d%d%d",&x,&y,&z);
          add(x,y,z);
          add(y,x,z);
      }
      spfa(0);
      int dian=0;
      for(int i=1;i<=n;i++){
        scanf("%d",&p[i]);
        dian+=p[i];
      }
      int sum=0,count=0;
      for(int i=1;i<=n;i++)
        if(dis[i]!=INF)
         sum+=dis[i];
     //   else count++;
      memset(dp,0,sizeof(dp));
      for(int i=1;i<=n;i++)
        for(int j=sum;j>=dis[i];j--)
          dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
      dian = dian/2+1;
      int ans=0;
      for(int i=0;i<=sum;i++)
        if(dp[i]>=dian)
        {
            ans=i;
            break;
        }
        if(ans)
            printf("%d\n",ans);
        else
            printf("impossible\n");
  }
  return 0;
}


In Action

标签:spfa+01背包

原文地址:http://blog.csdn.net/zhangweiacm/article/details/39293567

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