首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-09-10 
HDU3339In Action(01背包+Dijkstra算法)In ActionTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 3

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 DescriptionHDU   3339  In Action(01双肩包+Dijkstra算法)
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. 
InputThe 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. 
OutputThe minimal oil cost in this action.
If not exist print "impossible"(without quotes). 
Sample Input
22 30 2 92 1 31 0 2132 12 1 313
 
Sample Output
5impossible
 
AuthorLost@HDU 
SourceHDOJ 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];}}}}


 

热点排行