In Action

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

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

思路:题意是给你一张地图,上面有很多个电站,先求出起点的,最短路。然后开始DP(01背包问题)

问题是问你破坏一半总电量,行走的路程最短是多少

  1 #include <cstdio>
  2 #include <cstring>   
  3 #include <iostream>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <queue>
  8 using namespace std;
  9 
 10 const int maxn=110,maxm=20010,INF=10000000;
 11 struct qq
 12 {
 13     int n,to,z,ne;
 14     friend bool operator < (qq a,qq b)
 15     {
 16         return a.z>b.z;
 17     }
 18 } e[maxm],s,ya;
 19 
 20 priority_queue<qq> q;
 21 int d[maxn],node,x,y,z,cnt,to,n,m,h[maxn];
 22 bool f[maxn];
 23 int st[maxn],pp[maxn],ans,a[maxm],p[maxn];
 24 
 25 void addedge(int x,int y,int z)
 26 {
 27     cnt++;
 28     e[cnt].n=x;
 29     e[cnt].to=y;
 30     e[cnt].z=z;
 31     e[cnt].ne=h[x];
 32     h[x]=cnt;
 33 }
 34 
 35 void close()
 36 {
 37     exit(0);
 38 }
 39 
 40 void dijkstra(int st)
 41 {
 42     memset(f,false,sizeof(f));
 43     while (!q.empty())
 44         q.pop();
 45     f[st]=true;
 46     for (int i=0;i<=n;i++)
 47         d[i]=INF;
 48     d[st]=0;
 49     for (int p=h[st];p!=-1;p=e[p].ne)
 50     {
 51         s.n=st;
 52         s.to=e[p].to;
 53         s.z=e[p].z;
 54         q.push(s);
 55     }
 56     while (!q.empty())
 57     {
 58         s=q.top();
 59         q.pop();
 60         to=s.to;
 61         if (f[to]) continue;
 62         d[to]=s.z;
 63         f[to]=true;
 64         for (int p=h[to];p!=-1;p=e[p].ne)
 65         {
 66             node=e[p].to;
 67             if (not f[node])
 68             {
 69                 ya.n=to;
 70                 ya.to=node;
 71                 ya.z=d[to]+e[p].z;
 72                 q.push(ya);
 73             }
 74         }
 75     }
 76 }
 77 
 78 void init()
 79 {
 80     int T;
 81     scanf("%d",&T);
 82     while (T--)
 83     {
 84         while (scanf("%d %d",&n,&m)!=EOF)
 85         {
 86             memset(h,-1,sizeof(h));
 87             cnt=0;
 88             for (int i=1;i<=m;i++)
 89             {
 90                 scanf("%d %d %d",&x,&y,&z);
 91                 addedge(x,y,z);
 92                 addedge(y,x,z);
 93             }
 94             int st,en,maxpow=0;
 95             st=0;
 96             dijkstra(st);
 97             for (int i=1;i<=n;i++)
 98                 scanf("%d",&p[i]),maxpow+=p[i];
 99             for (int i=1;i<=maxpow;i++)
100                 a[i]=INF;
101             for (int i=1;i<=n;i++) //第几个电站
102             {
103                 for (int j=maxpow;j>=p[i];j--)
104                     if (a[j]>a[j-p[i]]+d[i])
105                         a[j]=a[j-p[i]]+d[i];
106             }
107             ans=INF;
108             for (int i=maxpow/2;i<=maxpow;i++)
109                 if (i>maxpow/2)
110                 {
111                     if (a[i]<ans)
112                         ans=a[i];
113                 }
114             if (ans!=INF)
115                 printf("%d\n",ans);
116             else
117                 printf("impossible\n");
118 
119         }
120     }
121 }
122 
123 
124 int main ()
125 {
126     init();
127     close();
128 }

 

posted on 2013-04-26 22:48  cssystem  阅读(136)  评论(0编辑  收藏  举报