In Action

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4720 Accepted Submission(s): 1553

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

开始没有读懂题意,就是有一个电网,每一个坦克可以控制一个电站,现在这些坦克都在0处,问怎样安排坦克去的电站使的耗油量最少并且能控制多于一半的电量.

SPFA求出0到每个点的距离,然后01背包;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef long long LL; const int MAX = 1e5+10; int n,m; int Map[110][110],Dp[10010]; int Dis[110]; bool vis[110]; int va[110]; void SPFA()//求最短路
{
memset(vis,false,sizeof(vis));
memset(Dis,INF,sizeof(Dis));
Dis[0]=0;
vis[0]=true;
queue<int>Q;
Q.push(0);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0;i<=n;i++)
{
if(Dis[u]+Map[u][i]<Dis[i])
{
Dis[i]=Map[u][i]+Dis[u];
if(!vis[i])
{
Q.push(i);
vis[i]=true;
}
}
}
vis[u]=false;
}
} int main()
{
int T;
int u,v,w;
int sum;
scanf("%d",&T);
while(T--)
{
memset(Map,INF,sizeof(Map));
scanf("%d %d",&n,&m);
sum=0;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
if(Map[u][v]>w)//去重
{
Map[u][v]=w;
Map[v][u]=w;
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&va[i]);
sum+=va[i];
}
SPFA();
memset(Dp,INF,sizeof(Dp));
Dp[0]=0;
for(int i=1;i<=n;i++)//01背包
{
for(int j=sum;j>=va[i];j--)
{
Dp[j]=min(Dp[j],Dp[j-va[i]]+Dis[i]);
}
}
int Max=INF;
for(int i=sum/2+1;i<=sum;i++)
{
if(Dp[i]<Max)
{
Max=Dp[i];
}
}
if(Max==INF)
{
printf("impossible\n");
}
else
{
printf("%d\n",Max);
}
}
return 0;
}

In Action(SPFA+01背包)的更多相关文章

  1. hdu 3339 In Action (最短路径+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. hdu3339 In Action(Dijkstra+01背包)

    /* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...

  3. HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】

     Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...

  4. HDU 3339 In Action【最短路+01背包】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...

  5. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  6. In Action(最短路+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  8. HDU-3339 IN ACTION(Dijkstra +01背包)

      Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the ...

  9. hdu 3339 In Action(迪杰斯特拉+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. mysql 常见的几个错误问题

    Mysql常见的几个错误问题及解决方法: 1.问题: mysql DNS反解:skip-name-resolve 错误日志有类似警告: 点击(此处)折叠或打开 120119 16:26:04 [War ...

  2. 解决Ueditor 不兼容IE7 和IE8

    引用Ueditor的js 的时候用 绝对路径

  3. Sikuli增强包

    一.前提准备1)  VCForPython27.msi2)  Cython安装包3)  pyjnius安装包4)  sikuli_cpython安装包5)  SLF4J文件包二.安装过程1)  VCF ...

  4. win8, VS2013 .NET 4.5在哪找svcutil.exe?

    我这个纠结呀,公司用win8, .NET 4.5.想做一个很简单的项目,就是wcf宿主iis,项目根目录下有aspx文件和svc文件.于是参考了一个博客http://www.cnblogs.com/y ...

  5. android命令安装apk时报错:INSTALL_FAILED_CPU_ABI_INCOMPATIBLE

    点击下载Genymotion-ARM-Translation.zip 将你的虚拟器运行起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你的虚拟机.

  6. hdu5354 Bipartite Graph

    分治+并查集.假设要求[L,mid]的答案,那么很明显,如果一条边的两个端点都>mid的话或者一个端点>mid一个端点<L,说明询问[L,mid]这个区间中任何一点时候,这一条边都是 ...

  7. MYSQL数据库自动本地/异地双备份/MYSQL增量备份

    构建高安全电子商务网站之(网站文件及数据库自动本地/异地双备份)架构图 继续介绍Linux服务器文件备份,数据库备份,数据安全存储相关的电子商务系统架构.针对安全性有多种多样的解决方案,其中数据备份是 ...

  8. 查看Linux服务器各种信息方法

    有的时候需要搜集服务器的各种信息,比如cpu信息,内存信息,linux版本信息,安装的各种软件信息等等.下面总结几种主要指标的查看方法. 1. 查看Linux发行版信息 [root@pcmweb ~] ...

  9. java dyn proxy

    package dyn; public interface RealService { void buy(); } =================== package dyn; public cl ...

  10. git批量删除分支

    要删除本地,首先要考虑以下三点 列出所有本地分支 搜索目标分支如:所有含有'dev'的分支 将搜索出的结果传给删除函数 所以我们可以得到: git br |grep 'dev' |xargs git ...