|
Posted on 2011-09-08 13:21 成幸毅 阅读(289) 评论(0) 编辑 收藏 引用
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;

 struct Edge {
int v,w,next;
}edge[100010];

const int INF = 0x3fffffff
int sps,head[101],w[101],dist[101];
bool inq[101],n,m;
int f[100001];

 void addedge(int u,int v, int w) {
edge[sps].v = v;
edge[sps].w = w;
edge[sps].next = head[u]
head[u] = sps++;
}

 void spfa() {
 for(int i = 0; i <= n; i++) {
dist[i] = ((i==0)?0:INF);
}
queue<int> q;
q.push(0);
memset(inq,0,sizeof(inq));
 while(!q.empty()) {
int u = q.front();q.pop();
inq[u] = false;
 for(int addr = head[u];addr != -1;addr = edge[addr].next) {
int v = edge[addr].v;
 if(dist[v] > dist[u] + edge[addr].w) {
dist[v] = dist[u] + edge[addr].w;
 if(!inq[v]) {
inq[v] = true;
q.push(v);
}
}
}
}
}

 int main() {
int cas;
scanf("%d",&cas);
 while(cas--) {
sps = 0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
int a,b,c;
int tot = 0;
int cnt = 0;
 for(int i = 0; i < m; i++) {
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
 for(int i = 1; i <= n; i++) {
scanf("%d",&w[i]);
cnt += w[i];
}
spfa();
int flag = 0;
 for(int i = 1; i <= n; i++) {
tot += dist[i];
 if(dist[i] == INF) { cout<<"impossible"<<endl;flag = 1;break;}
}
if(flag) continue;
for(int i = 0; i < 100001; i++)
f[i] = 0;
cnt = cnt/2 + 1;
 for(int i = 1; i <= n;i++) {
 for(int j = tot; j >= dist[i]; j--) {
f[j] = max(f[j],f[j-dist[i]] + w[i]);
}
}
 for(int i = 1; i <= tot; i++) {
 if(f[i] >= cnt) {
cout<<i<<endl;
break;
}
}
}
// system("pause");
return 0;
}

|