标签:
#include <stdio.h>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0xfffff
#define MAX 200
int path[MAX][MAX];
int dis[MAX];
bool sign[MAX];
int val[MAX];
int f[MAX * MAX];
int sum;
void initiaze(int n)
{
sum = 0;
for(int i=0; i<=n; i++)
{
sign[i] = false; dis[i] = INF;
for(int j=0; j<=n; j++)
{
path[i][j] = INF;
}
}
}
void input(int n, int m)
{
for(int i=0; i<n; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
{
if(path[a][b] > c)
{
path[a][b] = c;
path[b][a] = c;
}
}
}
for(int i=1; i<=m; i++)
{
scanf("%d", &val[i]);
sum += val[i];
}
}
void Spfa(int n, int start)
{
queue<int>q;
dis[start] = 0;
sign[start] = true;
q.push(start);
while(!q.empty() )
{
int tp = q.front();
q.pop();
for(int i=0; i<=n; i++)
{
if(dis[i] > dis[tp] + path[tp][i])
{
dis[i] = dis[tp] + path[tp][i];
if(!sign[i])
{
q.push(i);
sign[i] = true;
}
}
}
sign[tp] = false;
}
}
int main()
{
//freopen("read.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
int n, m;
scanf("%d%d", &n, &m);
initiaze(n);
input(m, n);
Spfa(n, 0);
memset(f, 0, sizeof(f) );
sum = (sum/2 +1);
int V = 0;
bool flag = 0;
for(int i=0; i<=m; i++) //以最短路径之和最为背包容量, 各个最短路径为放入背包的物体
{
V += dis[i];
if(dis[i] >= INF)
{flag =1; break;}
}
if(flag) {printf("impossible\n"); continue;}
for(int i=1; i<=n; i++)
{
for(int j=V; j>=dis[i]; j--)
{
f[j] = max(f[j], f[j-dis[i]]+val[i] ); //每一站的输电量最为放入背包的物品的价值
}
}
for(int i=1; i<=V; i++)
{
if(f[i] >= sum)
{
printf("%d\n", i);
break;
}
}
}
return 0;
}
/*
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 vals, which forms a huge and complex electric network. Every electric val 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 val, 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 vals(the IDs are 1,2,3...n), and the number of the roads between the val(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 val‘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
*/
标签:
原文地址:http://www.cnblogs.com/sober-reflection/p/9556ce57776f645cfdc5ef9b6b106233.html