HDU 3339 In Action(01背包+Dijkstra算法)

发布时间:2016-12-7 0:01:34 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"HDU 3339 In Action(01背包+Dijkstra算法)",主要涉及到HDU 3339 In Action(01背包+Dijkstra算法)方面的内容,对于HDU 3339 In Action(01背包+Dijkstra算法)感兴趣的同学可以参考一下。

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3078    Accepted Submission(s): 980 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 [email protected]   Source HDOJ Monthly Contest – 2010.03.06     思路: 01背包+Dijkstra算法   import java.io.*; import java.util.*; public class Main { int T, n, m, MAX = 50000, M = 105, sum = 0, sum1 = 0; int[][] map = new int[M][M]; int[] dis = new int[MAX]; boolean[] boo = new boolean[MAX]; int[] pow = new int[MAX]; int[] dp = new int[1000001]; public static void main(String[] args) throws Exception{ new Main().work(); } void work() throws Exception{ BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out),true); T = Integer.parseInt(bu.readLine()); String str[]; while (T-- != 0) { str=bu.readLine().split(" "); n = Integer.parseInt(str[0]); m = Integer.parseInt(str[1]); init(); for (int i = 0; i < m; i++) { str=bu.readLine().split(" "); int a = Integer.parseInt(str[0]); int b = Integer.parseInt(str[1]); int c = Integer.parseInt(str[2]); if (map[a][b] >= c) map[a][b] = map[b][a] = c; } Arrays.fill(pow, 0); for (int i = 1; i <= n; i++) { pow[i] = Integer.parseInt(bu.readLine()); } dijstra(0); if(dis[1]==MAX){ pw.println("impossible"); continue; } int dsum=0; int psum=0; for(int i=1;i<=n;i++){ psum+=pow[i]; dsum+=dis[i]; } Arrays.fill(dp, 0); for (int i = 1; i <= n; i++) { for (int j = dsum; j >= dis[i]; j--) { dp[j] = Math.max(dp[j],dp[j - dis[i]] + pow[i]); } } psum=(psum>>1)+1; for(int i=1;i<=dsum;i++){ if(dp[i]>=psum){ pw.println(i); break; } } } } void init() { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { map[i][j] = MAX; } } } void dijstra(int v) { int k = 0; for (int i = 0; i <= n; i++) { boo[i] = false; dis[i] = map[v][i]; } boo[v] = true; dis[v] = 0; for (int i = 0; i <= n; i++) { int min = MAX; for (int j = 0; j <= n; j++) { if (!boo[j] && dis[j] < min) { min = dis[j]; k = j; } } if (min == MAX) break; boo[k] = true; sum1+=dis[k]; for (int j = 0; j <= n; j++) { if (!boo[j] && dis[j] > dis[k] + map[k][j]) dis[j] = dis[k] + map[k][j]; } } } }  

上一篇:Cypress固件架构彻底解析及USB枚举
下一篇:设计一个例子,演示PCTUSED和PCTFREE对数据操作的影响

相关文章

相关评论