In Action

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;
}
时间: 2024-10-29 06:29:12

In Action的相关文章

action

package com.ivchat.park.coupons.action; import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List; import javax.annotation.Resource; import net.sf.json.JSONObject; import org.apache.struts2.convention.an

【机器学习实战】Machine Learning in Action 代码 视频 项目案例

MachineLearning 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远 Machine Learning in Action (机器学习实战) | ApacheCN(apache中文网) 视频每周更新:如果你觉得有价值,请帮忙点 Star[后续组织学习活动:sklearn + tensorflow] ApacheCN - 学习机器学习群[629470233] 第一部分 分类 1.) 机器学习基础 2.) k-近邻算法 3.) 决策树 4.) 基于概率论的分类方法:朴素

wex5 教程 自定义action与名称去重

有一个订单,需要取出不同的客户名称,且只显示一次名称.效果如下图: 一 思路:自定义action,用sql语句的group by分组,将名称返回前端,用list显示出来. 二 制作步骤: 1 新建service service作为后端服务分发接口,一个工程可以有多个service,一个service可以有多个action. 2  新建action action基本参数: 名称:自定义 实现:(指向 java方法) 格式为 Name.getName   其中Name为java类,首写字母必须为大写

day8——ajax传参到action(Struts2)

第一种:url+?+参数 jsp中: $(function(){ $("[name='delemp']").click(function(){ $this = $(this); $delid = $this.attr("delid"); if(confirm("确认删除该条数据吗?")){ $.ajax({ type:"get", url:"deleteemployeebyid?delid="+$delid

Struts2系列笔记(3)---Action类的3种书写方式

Action类的3种书写方式 本文主要写有关写Action类的3种书写方式: (1)第一种 Action可以是POJO (简单模型对象)  不需要继承任何父类 也不需要实现任何接口 (2)实现Action接口 (3)继承ActionSupport(推荐) 那我们先来书写第一种: (1)第一种 Action可以是POJO (简单模型对象)  不需要继承任何父类 也不需要实现任何接口 1 //这里其实就是一个普通的类,类里面的方法可以任意写,如果写execute()方法那就代表默认执行它 2 pub

Func与Action

Func与Action C#委托的介绍(delegate.Action.Func.predicate) Func和Action委托的区别和简单使用

SharePoint 2010/SharePoint 2013 Custom Action: 基于Site Collection 滚动文字的通知.

应用场景: 有时候我们的站点需要在每个页面实现滚动文字的通知,怎么在不修改Master Page的情况下实现这个功能?我们可以使用Javascript 和 Custom Action 来实现. 创建一个Custom Action.主要使用到 Location = 'ScriptLink' 属性, 该属性可以动态的加载JavaScript 文件链接和代码块到模板页.代码如下: <Elements xmlns="http://schemas.microsoft.com/sharepoint/&

译-BMC Remedy Action Request System权限控制概述

原文链接:Access control overview 说明: BMC Remedy Action Request System是BMC ITSM产品平台,简称AR 或者Remedy,可实现基于ITIL标准的整个IT管理流程的实施定制.该平台可实现多种权限级别的管理,包括人员.组.角色,以及表.字段.行级别等.本文可以用作其他对权限要求比较精细的系统参考. 为了便于理解,部分名词翻译如下: Server:服务器Form (or table):表单Field (or column):字段Acti

Machine Learning In Action 第二章学习笔记: kNN算法

本文主要记录<Machine Learning In Action>中第二章的内容.书中以两个具体实例来介绍kNN(k nearest neighbors),分别是: 约会对象预测 手写数字识别 通过“约会对象”功能,基本能够了解到kNN算法的工作原理.“手写数字识别”与“约会对象预测”使用完全一样的算法代码,仅仅是数据集有变化. 约会对象预测 1 约会对象预测功能需求 主人公“张三”喜欢结交新朋友.“系统A”上面注册了很多类似于“张三”的用户,大家都想结交心朋友.“张三”最开始通过自己筛选的

untiy3d action管理机制的编写

使用unity3d对于一些可视化强迫者来说,是一个不错的选择,但unity3d没有cocos2d的action管理机制,比如cocos2dx的CCMoveTo,CCScale等action,所以笔者通过封装action管理来实现类似cocos2dx的actionmanager. 首先需要写一个ActionManager来创建.更新.移除所有action.编写代码实现如下: using UnityEngine;using System.Collections;using System; publi