hdu 3339 In Action(迪杰斯特拉+01背包)
2016-05-05 20:34
381 查看
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5102 Accepted Submission(s):
1696
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The minimal oil cost in this action.
If not exist
print "impossible"(without quotes).
[align=left]Sample Input[/align]
2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3
[align=left]Sample Output[/align]
5
impossible
[align=left]Author[/align]
Lost@HDU
[align=left]Source[/align]
HDOJ
Monthly Contest – 2010.03.06
[align=left]Recommend[/align]
lcy | We have carefully selected several similar
problems for you: 1385 3338 1102 3342 1598
先用迪杰斯特拉求出从0点到各点的最短距离,由于需要大于一半总能量,所以需要判断是否取这个点,使用01背包解决。
题意:第一个数为数据组数,然后输入n,m,代表有n个发电站,m条路,然后给出m条路和该路消耗的油,然后是n行,每行是相应的发电站的能量,要注意,每个发电站必须停一辆坦克才能控制这个发电站,只有控制的总能量超过一半能使其瘫痪
附上代码:
#include <iostream> #include <cstdio> #include <cstring> #define M 105 #define inf 0x3f3f3f3f using namespace std; int map[M][M],dis[M],vis[M]; int dp[10005]; int mins(int a,int b) { return a>b?b:a; } int main() { int i,j,n,m,T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(dis,0,sizeof(dis)); memset(vis,0,sizeof(vis)); for(i=0; i<=n; i++) for(j=0; j<=n; j++) if(i==j) map[i][j]=0; else map[i][j]=inf; int a,b,c; while(m--) { scanf("%d%d%d",&a,&b,&c); if(map[a][b]>c) map[a][b]=map[b][a]=c; } vis[0]=1; for(i=0; i<=n; i++) dis[i]=map[0][i]; int min,t; for(i=0; i<=n; i++) //迪杰斯特拉 { min=inf; for(j=0; j<=n; j++) if(!vis[j]&&min>dis[j]) { min=dis[j]; t=j; } vis[t]=1; for(j=0; j<=n; j++) if(!vis[j]&&map[t][j]<inf) if(dis[j]>dis[t]+map[t][j]) dis[j]=dis[t]+map[t][j]; } int s[M],sum=0; for(i=1; i<=n; i++) { scanf("%d",&s[i]); sum+=s[i]; } for(i=1; i<=sum; i++) dp[i]=inf; dp[0]=0; for(i=1; i<=n; i++) //01背包 for(j=sum; j>=s[i]; j--) dp[j]=mins(dp[j],dp[j-s[i]]+dis[i]); int x=sum/2+1,mm=inf; for(i=x; i<=sum; i++) //选出最小的距离 if(dp[i]<mm) mm=dp[i]; if(mm<inf) printf("%d\n",mm); else printf("impossible\n"); } return 0; }
相关文章推荐
- njust 1925 sequence 拆分序列,寻找非降序列的最小个数,不变序列顺序。
- Unity3D场景切换
- js 控制 checkbox不选中
- struts.properties配置详解
- [Assignment] C++1
- 内部类
- Static2
- HDU 3836 Equivalent Sets
- 多态
- 19.栈的压入、弹出序列
- iOS项目上线流程视频分享
- 二叉树的下一个结点
- 静态分析分析
- 接口
- 加分二叉树 树形DP
- WY自动部署平台使用
- 使用纯java基本语言实现泰勒展开的sin和cos计算
- Javascript 之内置对象
- 抽象类的概念
- ViewPagerIndictor框架的使用(第一部分)