6.2.8 In Action

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 285    Accepted Submission(s): 121

 
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
 
最短路徑+01揹包
 
#include<cstdio>
#include<cstring>
#include<utility>
#include<queue>
#define INF 1e8
using namespace std;

typedef pair<int, int> pii;
const int MAXN = 100 + 10;
const int MAXM = 2 * 10000 + 10;
int d[MAXN], done[MAXN], first[MAXN];  //dijkstra輔助數組
int u[MAXM], v[MAXM], w[MAXM], next[MAXM];
int p[MAXN], f[10001];	//p記錄每個電站的電量 ,f揹包問題的輔助數組 .(剛開始f數組開小了,跪好久) 
int n, m;


void init(){
	for(int i = 0; i <= n; i++){
		first[i] = -1;
		done[i] = 0;
		d[i] = INF;
	}
}
void dijkstra(int st){
	d[st] = 0;
	priority_queue<pii, vector<pii>, greater<pii> > q;
	q.push(make_pair(d[st], st));
	while(!q.empty()){
		pii p = q.top();
		q.pop();
		int u = p.second;
		if(done[u]) continue;
		done[u] = 1;
		for(int e = first[u]; e != -1; e = next[e]){
			if(d[v[e]] > d[u] + w[e]){
				d[v[e]] = d[u] + w[e];
				q.push(make_pair(d[v[e]], v[e]));
			}
		}
	}
	
} 

int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &n, &m);
		int i, j, a, b, c, index = 0;
		init();
		for(i = 0; i < m; i++){
			scanf("%d%d%d", &a, &b, &c);
			u[index] = a;
			v[index] = b;
			w[index] = c;
			next[index] = first[u[index]];
			first[u[index]] = index;
			index++;
			u[index] = b;
			v[index] = a;
			w[index] = c;
			next[index] = first[u[index]];
			first[u[index]] = index;
			index++;
		}
		int sump = 0, sumo = 0;
		int flag = 0; //是否能夠摧毀,0表示不能 
		for(i = 1; i <= n; i++){
			scanf("%d", &p[i]);
			sump += p[i];
		}
		sump = (sump >> 1) + 1;
		dijkstra(0);
		for(int i = 1; i <= n; i++) {
			if(d[i] < INF)
				sumo += d[i];
		}		
		//ZeroOnePack
		memset(f, 0, sizeof(f));
		for(i = 1; i <= n; i++){
			for(j = sumo; j >= d[i]; j--){
				f[j] = max(f[j], f[j-d[i]] + p[i]);
			}			
		}
		for(i = 1; i <= sumo; i++){
			if(f[i] >= sump){
				flag = 1;
				break;
			}
		}			
		if(flag) printf("%d\n", i);
		else printf("impossible\n");
	}
	return 1;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章