集训队专题(3)1012 In Action

来源:互联网 发布:手机唱歌调音软件 编辑:程序博客网 时间:2024/04/28 21:47

In Action

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


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
22 30 2 92 1 31 0 2132 12 1 313
 

Sample Output
5impossible
 

Author
Lost@HDU
 

Source
HDOJ Monthly Contest – 2010.03.06
 

此题又是一个考验英文和耐心的题……

题意:要破坏掉一个电网, 有n个电站编号为1~n,每个电站有它自己的能量值。有一个军事基地编号为0,里面有无限个坦克,可以开到某个电站轰炸破坏掉电站,并且一个坦克只能破坏一个。现在要破坏掉其中一些电站,要让电网的总能量值损失一半以上, 并且要让所有执行任务的坦克去目的地路费最少。

不知道读者是不是和小编一样,刚拿到这题的时候一点思路都没有。

仔细思考,这题可以先求出从起点到各个点的最短距离,再将这些最短距离作为每个点的权值,求在电网的总能量值损失达到一半是权值和的最小值。此时问题便转换成了01背包问题,所以这题的解法为最短路径+01背包。

#include <cstdio>  #include <cstring>  #include <algorithm>  #include <queue>  #define INF 0x3f3f3f3f  #define MAXN 500000  #define MAXM 20010   using namespace std;  struct Edge{      int from, to, val, next;  }edge[MAXM]; int head[110], edgenum;int n,m;void init(){      edgenum = 0;      memset(head,-1,sizeof(head));  }  int p[110];  void addEdge(int u, int v, int w)  {      Edge E1 = {u, v, w, head[u]};      edge[edgenum] = E1;      head[u] = edgenum++;  }  int sum;  void getMap()  {      scanf("%d%d",&n,&m);     init();      for(int i = 0; i < m; i++)      {          int a, b, c;          scanf("%d%d%d",&a,&b,&c);        addEdge(a, b, c);          addEdge(b, a, c);      }      sum = 0;      for(int i = 1; i <= n; i++)      {    scanf("%d",&p[i]);sum += p[i];    }  }  int dist[MAXN]; bool vis[MAXN];  int D;  void SPFA(int s)  {      queue<int> Q;      vis[s] = true, dist[s] = 0;      Q.push(s);      while(!Q.empty())      {          int u = Q.front();          Q.pop();          vis[u] = false;          for(int i = head[u]; i != -1; i = edge[i].next)          {              Edge E = edge[i];              if(dist[E.to] > dist[u] + E.val)              {                  dist[E.to] = dist[u] + E.val;                  if(!vis[E.to])                  {                      vis[E.to] = true;                      Q.push(E.to);                  }              }          }      }      D = 0;      for(int i = 1; i <= n; i++)          D += dist[i]==INF? 0:dist[i];  }  int dp[MAXN];  void solve()  {      getMap();memset(vis,false,sizeof(vis));memset(dist,INF,sizeof(dist));      SPFA(0);memset(dp,0,sizeof(dp));      sum = sum / 2 + 1;      int ans = INF;      for(int i = 1; i <= n; i++)      {          for(int j = D; j >= dist[i]; j--)          {              dp[j] = max(dp[j],dp[j-dist[i]]+p[i]);              if(dp[j] >= sum && j < ans)                  ans = j;          }                    }      if(ans == INF)          printf("impossible\n");      else          printf("%d\n",ans);  }  int main()  {      int t;scanf("%d",&t);      while(t--){          solve();      }      return 0;  }


0 0