Time Limit: 20 Sec
Memory Limit: 256 MB
http://acm.hdu.edu.cn/showproblem.php?pid=3339
Input
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
题意
给你一个图,每个图都有权值,然后让你派出多辆坦克,破环至少占权值一半的城市,派出坦克的代价就是从0点到那些城市的距离
求最少代价
题解:
点数很少,注意有重边
直接跑一发flyod,然后再来个背包dp
然后就好了~
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** int dis[110][110]; int dis1[110]; int power[110]; int dp[maxn]; int main() { //test; int t=read(); for(int cas=1;cas<=t;cas++) { int n=read(),m=read(); for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dis[i][j]=inf; for(int i=0;i<m;i++) { int a=read(),b=read(),c=read(); dis[a][b]=dis[b][a]=min(dis[a][b],c); } int sum,aim; sum=aim=0; for(int i=1;i<=n;i++) { power[i]=read(); aim+=power[i]; } for(int k=0;k<=n;k++) { for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } } } for(int i=1;i<=n;i++) { dis1[i]=dis[0][i]; if(dis1[i]<inf) sum+=dis1[i]; } memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=sum;j>=dis1[i];j--) dp[j]=max(dp[j],dp[j-dis1[i]]+power[i]); int ans=inf; for(int i=0;i<=sum;i++) { if(dp[i]>=(aim/2+1)&&dp[i]<ans) { ans=i; break; } } if(ans>=inf) printf("impossible\n"); else printf("%d\n",ans); } }