In Action |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 169 Accepted Submission(s): 78 |
Problem Description
![]() |
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 |
Author
Lost@HDU
|
Source
HDOJ Monthly Contest – 2010.03.06
|
Recommend
lcy
|
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> using namespace std; #define maxn 0xfffffff int vis[110], d[110], p[110]; int dp[10010]; typedef pair<int, int> pii; typedef struct S { int v, w; struct S *next; } EDGE; EDGE *node[110], edge[20010]; char op[2]; int m, n, cnt; void add(int a, int b, int c) { edge[cnt].v = b; edge[cnt].w = c; edge[cnt].next = node[a]; node[a] = &edge[cnt++]; } int min(int a, int b) { return a < b ? a : b; } int main() { int i, j, a, b, c, t, ans; int T, sum; pii u; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); memset(vis, 0, sizeof (vis)); for (i = 0; i <= n; ++i) { d[i] = maxn; node[i] = NULL; } d[0] = 0; cnt = 0; sum = 0; while (m--) { scanf("%d%d%d", &a, &b, &c); add(a, b, c); add(b, a, c); } for (i = 1; i <= n; ++i) { scanf("%d", &p[i]); sum += p[i]; } for (i = 1; i <= sum; ++i) dp[i] = maxn; dp[0]=0; priority_queue<pii, vector<pii>, greater<pii> > q; q.push(make_pair(d[0], 0)); while (!q.empty()) { u = q.top(); q.pop(); a = u.second; vis[a] = 1; for (; node[a]; node[a] = node[a]->next) { b = node[a]->v; if (!vis[b] && d[a] + node[a]->w < d[b]) { d[b] = d[a] + node[a]->w; q.push(make_pair(d[b], b)); } } } for (i = 1; i <= n; ++i) { for (j = sum; j >= p[i]; --j) { dp[j] = min(dp[j], dp[j - p[i]] + d[i]); } } ans = maxn; for (i = sum / 2 + 1; i <= sum; ++i) { if (dp[i] == maxn) continue; if (dp[i] < ans) ans = dp[i]; } if (ans == maxn) printf("impossible\n"); else printf("%d\n", ans); } return 0; }