标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4764 Accepted Submission(s): 1569
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 const int INF=0x3f3f3f3f; 9 const int MAXN=110; 10 const int MAXM=10010<<1; 11 struct Node{ 12 int u,v,w; 13 }; 14 Node dt[MAXM]; 15 int edgnum,n; 16 int dis[MAXN]; 17 int bag[10010]; 18 int power[MAXN]; 19 int add(int u,int v,int w){ 20 dt[edgnum].u=u;dt[edgnum].v=v;dt[edgnum++].w=w; 21 } 22 void Bellman(){ 23 mem(dis,INF);dis[0]=0; 24 for(int j=0;j<=n;j++){ 25 for(int i=0;i<edgnum;i++){ 26 int u=dt[i].u,v=dt[i].v,w=dt[i].w; 27 dis[v]=min(dis[v],dis[u]+w); 28 } 29 } 30 } 31 int main(){ 32 int m,T; 33 scanf("%d",&T); 34 while(T--){ 35 edgnum=0; 36 scanf("%d%d",&n,&m); 37 int u,v,w; 38 while(m--){ 39 scanf("%d%d%d",&u,&v,&w); 40 add(u,v,w);add(v,u,w); 41 } 42 mem(bag,0); 43 int V=0,sum=0; 44 for(int i=1;i<=n;i++)scanf("%d",power+i),V+=power[i]; 45 int flot=0; 46 Bellman(); 47 for(int i=1;i<=n;i++){ 48 if(dis[i]==INF){ 49 continue; 50 } 51 sum+=dis[i]; 52 } 53 for(int i=1;i<=n;i++){ 54 if(dis[i]!=INF) 55 for(int j=sum;j>=dis[i];j--){ 56 bag[j]=max(bag[j],bag[j-dis[i]]+power[i]); 57 } 58 } 59 int i; 60 for(i=0;i<sum;i++)if(bag[i]>V/2){//不能是(v+1)/2。。。 61 flot=1; 62 break; 63 } 64 if(flot)printf("%d\n",i); 65 else puts("impossible"); 66 } 67 return 0; 68 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4928291.html