首页 > 代码库 > In Action
In Action
Problem Description
[img]http://acm.hdu.edu.cn/data/images/C235-1007-1.jpg[/img] 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#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int INF = 2000000000; const int MX = 10010; struct node { int w,v,next; }s[MX<<2]; int n,m,cnt; int head[MX],dis[MX],p[10010],dp[10010]; void Init() { cnt=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w) { s[cnt].v=v; s[cnt].w=w; s[cnt].next=head[u]; head[u]=cnt++; } void spfa(int ss) { int i,v,w,tmp; bool vis[MX]; queue<int>q; for(i=0;i<=n;i++) { dis[i]=INF; vis[i]=false; } dis[ss]=0; q.push(ss); while(!q.empty()) { tmp = q.front(); q.pop(); vis[tmp]=false; for(i=head[tmp];i!=-1;i=s[i].next) { v = s[i].v; w = s[i].w; if(dis[v]>dis[tmp]+w) { dis[v]=dis[tmp]+w; if(!vis[v]) { vis[v]=true; q.push(v); } } } } } int main() { int x,y,z,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); Init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } spfa(0); int dian=0; for(int i=1;i<=n;i++){ scanf("%d",&p[i]); dian+=p[i]; } int sum=0,count=0; for(int i=1;i<=n;i++) if(dis[i]!=INF) sum+=dis[i]; // else count++; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=sum;j>=dis[i];j--) dp[j]=max(dp[j],dp[j-dis[i]]+p[i]); dian = dian/2+1; int ans=0; for(int i=0;i<=sum;i++) if(dp[i]>=dian) { ans=i; break; } if(ans) printf("%d\n",ans); else printf("impossible\n"); } return 0; }
In Action
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。