先上题目:
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3352 Accepted Submission(s): 1068
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.
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).
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
题意:有一堆电站(n个),你有一个基地(0号),每个电站有一点的供电量,给你关于电站和你的基地的无向图,现在需要你控制一部分电站,使得控制的电站的供电量大于总供电量的一半,同时由于控制一个电站需要的一辆坦克,一辆坦克没有一个单位的距离需要一单位的油,问能否可以达到要求,如果可以达到要求,最少需要多少的油。
这是一题最短路+01背包。首先求出到每一个电站的距离,然后总供电量作为背包的容量,跑一次01背包以后在大于一般供电量的那个供电量开始向总供电量方向扫描找出最小值,这里需要注意的是一定是大于总供电量的一半。
这题因为敲的时候因为有一个地方将+=写成=+,结果算上另一个小错误加起来WA了10+次(┬_┬) 。
上代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #define MAX 100005 5 #define LL int 6 #define MAXN 105 7 #define INF 99999999 8 #define max(x,y) (x>y ? x : y) 9 #define min(x,y) (x<y ? x : y) 10 #define DEBUG(x) printf("Line: %4 ",x) 11 using namespace std; 12 13 typedef struct{ 14 int to; 15 int l; 16 int next; 17 }edge; 18 19 int p[MAXN],tot; 20 edge e[MAX]; 21 22 void add(int u,int v,int l){ 23 e[tot].to=v; 24 e[tot].l=l; 25 e[tot].next=p[u]; 26 p[u]=tot++; 27 } 28 29 30 int map[MAXN][MAXN]; 31 32 void reset(){ 33 memset(p,-1,sizeof(p)); 34 memset(e,0,sizeof(e)); 35 tot=0; 36 //memset(map,-1,sizeof(map)); //邻接矩阵用 37 } 38 39 LL dis[MAXN]; 40 int n,m; 41 42 43 bool vin[MAXN]; 44 45 void spfa(){ 46 queue<int> q; 47 dis[0]=0; 48 memset(vin,0,sizeof(vin)); 49 for(int i=1;i<=n;i++){ 50 dis[i]=INF; 51 } 52 q.push(0); 53 vin[0]=1; 54 while(!q.empty()){ 55 int u=q.front(); 56 vin[u]=0; 57 q.pop(); 58 for(int v=p[u];v!=-1;v=e[v].next){ 59 //DEBUG(43); 60 int l=dis[u]+e[v].l; 61 if(dis[e[v].to]>l){ 62 dis[e[v].to]=l; 63 if(!vin[e[v].to]){ 64 q.push(e[v].to); 65 vin[e[v].to]=1; 66 } 67 } 68 } 69 //DEBUG(52); 70 } 71 } 72 73 //dij 74 /* 75 bool flag[MAXN]; 76 void dij(int r){ 77 int u; 78 LL minn; 79 memset(flag,0,sizeof(flag)); 80 for(int i=0;i<=n;i++){ 81 dis[i]=INF; 82 } 83 dis[r]=0; 84 for(int i=0;i<=n;i++){ 85 u=-1; 86 minn=INF; 87 for(int j=0;j<=n;j++){ 88 if(!flag[j] && minn>dis[j]){ 89 u=j; 90 minn=dis[j]; 91 } 92 } 93 if(u==-1) return ; 94 flag[u]=1; 95 96 //for(int v=p[u];v!=-1;v=e[v].next){ 97 // dis[e[v].to]=min(dis[e[v].to],dis[u]+e[v].l); 98 // } 99 100 for(int i=0;i<=n;i++){ 101 if(map[u][i]!=-1){ 102 dis[i]=min(map[u][i]+dis[u],dis[i]); 103 } 104 } 105 } 106 } 107 */ 108 int w[MAXN]; 109 LL dp[MAX]; 110 LL minn,wsum; 111 112 int main() 113 { 114 int t; 115 int st,ed,l; 116 //freopen("data.txt","r",stdin); 117 scanf("%d",&t); 118 while(t--){ 119 scanf("%d %d",&n,&m); 120 reset(); 121 for(int i=0;i<m;i++){ 122 scanf("%d %d %d",&st,&ed,&l); 123 add(st,ed,l); 124 add(ed,st,l); 125 /* 126 if(map[st][ed]==-1 || map[st][ed]>l){ 127 map[st][ed]=l; 128 map[ed][st]=l; 129 } 130 */ 131 } 132 wsum=0; 133 for(int i=1;i<=n;i++){ 134 scanf("%d",&w[i]); 135 wsum+=w[i]; 136 } 137 spfa(); 138 //dij(0); 139 for(int i=0;i<=wsum;i++) dp[i]=INF; 140 dp[0]=0; 141 for(int i=1;i<=n;i++){ 142 for(int j=wsum;j>=w[i];j--){ 143 dp[j]=min(dp[j-w[i]]+dis[i],dp[j]); 144 //printf("%d ",dp[j]); 145 } 146 //printf(" "); 147 } 148 minn=INF; 149 for(LL i=(wsum)/2+1;i<=wsum;i++){ 150 minn=min(dp[i],minn); 151 } 152 if(minn!=INF) printf("%d ",minn); 153 else printf("impossible "); 154 } 155 156 return 0; 157 }