In Action

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

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点出发,去破坏每一个电站,每个坦克只能破坏一个电站,每个电站都有一定的电量,问至少破坏1/2的电量最少需要消耗多少油量。

题解:先求出从0点出发到每一个顶点的最短距离,然后把从0出发到每一个顶点的距离之和当作容量v,进行01背包。最后和电量的总和的1/2比较。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct edge
{
int to,cost;
friend bool operator < (edge A,edge B)
{
return A.cost>B.cost;
}
};
int n,m;
int dp[maxn*maxn];
vector<edge> g[maxn];
bool done[maxn];
int d[maxn];
int v[maxn];
void dijkstra()
{
memset(done,false,sizeof(done));
memset(d,INF,sizeof(d));
d[] = ;
priority_queue<edge> q;
q.push((edge){,});
while(!q.empty())
{
edge cur = q.top();
q.pop();
int v = cur.to;
if(done[v]) continue;
done[v] = true;
for(int i = ; i<g[v].size(); i++)
{
cur = g[v][i];
if(d[cur.to]>d[v]+cur.cost)
{
d[cur.to] = d[v]+cur.cost;
q.push((edge){cur.to,d[cur.to]});
}
}
}
}
void solve()
{
scanf("%d %d",&n,&m);
int a,b,c;
for(int i = ; i<=m; i++)
{
scanf("%d %d %d",&a,&b,&c);
g[a].push_back((edge){b,c});
g[b].push_back((edge){a,c});
}
dijkstra();
double sum = ;
int count = ;
memset(dp,,sizeof(dp));
for(int i = ; i<=n; i++)
{
scanf("%d",&v[i]);
sum += v[i];
if(d[i]!=INF)
count += d[i];
}
for(int i = ; i<=n; i++)
{
for(int j = count; j>=d[i]; j--)
{
if(dp[j-d[i]]+v[i]>dp[j])
{
dp[j] = dp[j-d[i]]+v[i];
}
}
}
int flag = ;
for(int i = ; i<=count; i++)
{
if(dp[i]>sum/2.0)
{
printf("%d\n",i);
flag = ;
break;
}
}
if(flag == ) printf("impossible\n");
for(int i = ; i<=n; i++) g[i].clear();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
solve();
}
return ;
}

HDU 3339 最短路+01背包的更多相关文章

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

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

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

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

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

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

  4. *HDU3339 最短路+01背包

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

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

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

  6. HDU 2602 Bone Collector(01背包裸题)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 2546 饭卡(01背包裸题)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  8. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  9. HDU 5234 Happy birthday 01背包

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/con ...

随机推荐

  1. HDU2519:新生晚会

    Problem Description 开学了,杭电又迎来了好多新生.ACMer想为新生准备一个节目.来报名要表演节目的人很多,多达N个,但是只需要从这N个人中选M个就够了,一共有多少种选择方法?   ...

  2. error while loading shared libraries: libmcrypt.so.4

    /usr/local/php/sbin/php-fpm: error while loading shared libraries: libmcrypt.so.4: cannot open share ...

  3. java regex possissive relunctant

    Java正则表达中Greedy Reluctant Possessive 的区别 分类: java 2015-01-16 00:28 1588人阅读 评论(9) 收藏 举报 正则表达式Java 目录( ...

  4. 产生一个int数组,长度为100,并向其中随机插入1-100,不重复

    #define RANDOM(X) (rand() % X + 1) int main() { //标志数组 ] = {}; ] = {}; //默认的随机数种子是1,这样的话,每次执行这个程序都会得 ...

  5. [转]c++ new带括号和不带括号

    ref:http://m.blog.csdn.net/blog/u012745772/42420443 在new对象的时候有加上(),有不加(),不知道这个到底是什么区别?比如:CBase *base ...

  6. js的各种错误类型

    1.SyntaxError(语法错误) 解析代码时发生的语法错误 eg:var 1a; Uncaught SyntaxError: Unexpected number 2.ReferenceError ...

  7. python gui编程

    1.准备工作 下载PyCharm 3.4(选择他的原因:1.有破解版,2.它的默认风格是PEB风格,一旦不符合PEB风格就会有提示) 下载并且安装界面设计工具wxFormBuilder 给python ...

  8. C++中的函数指针和函数对象总结

    篇一.函数指针函数指针:是指向函数的指针变量,在C编译时,每一个函数都有一个入口地址,那么这个指向这个函数的函数指针便指向这个地址.函数指针的用途是很大的,主要有两个作用:用作调用函数和做函数的参数. ...

  9. group_concat()函数总结

    group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果.比较抽象,难以理解. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组, ...

  10. VMI

    在虚拟机外部监控虚拟机内部运行状态的方法被称为虚拟机自省(Virtual Machine Introspection,VMI).VMI允许特权域查看非特权域的运行状态,并能获得被监控虚拟机运行状况相关 ...