博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3001 Travelling 【状态压缩DP】
阅读量:7098 次
发布时间:2019-06-28

本文共 2304 字,大约阅读时间需要 7 分钟。

Problem Description
After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
Input
There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
Sample Input
2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
Sample Output
100
90
7
code:
View Code
#include
#include
#define min(a,b)(a)<(b)?(a):(b) #define inf 0x1f1f1f1f int dp[60000][12]; // dp[i][j] 状态为 i 的情况下 到达 j 点的最短路 int dis[12][12]; int f[60000][11]; // f[i][j] 状态 i 的情况下 i 的 第 j 位 int fac[12]; int main() {
int va,ns,t,x,i,j,s,p,q,len,n,m,res; fac[1]=1; for(i=2;i<11;i++) fac[i]=fac[i-1]*3; for(i=0;i<59050;i++){
t=i; for(j=1;j<=10;j++){
f[i][j]=t%3; t/=3; if(t==0) break; } } //freopen("D:ce.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) {
res=inf; memset(dis,inf,sizeof(dis)); while(m--) {
scanf("%d%d%d",&p,&q,&len); if(len
=2) continue; ns=s+fac[j]; dp[ns][j]=min(dp[s][i]+dis[i][j],dp[ns][j]); } } // 如果能 到达所有点 if(va){
for(j=1;j<=n;j++) res=min(dp[s][j],res); } } if(res==inf){
printf("-1\n"); continue; } printf("%d\n",res); } return 0; }

转载于:https://www.cnblogs.com/dream-wind/archive/2012/04/01/2428279.html

你可能感兴趣的文章
第十二章——SQLServer统计信息(1)——创建和更新统计信息
查看>>
立体匹配导论
查看>>
ServiceStack.Hello——跨平台.net REST api服务搭建
查看>>
增加网站点击(引流)的不外传seo技巧
查看>>
转载:Expression 表达式树学习整理
查看>>
jvm系列(五):Java GC 分析
查看>>
在Docker Toolbox和Boot2Docker中使用Volume Plugins
查看>>
【独家】一文读懂文字识别(OCR)
查看>>
安卓程序员要拿到5000和1w的薪资,分别需要掌握哪些技术?
查看>>
浅谈机器视觉技术未来发展趋势
查看>>
[译] 前端调试技巧与诀窍
查看>>
从零开始写linux字符设备驱动程序(一)(基于友善之臂tiny4412开发板)
查看>>
ASP.NET MVC Model元数据及其定制:一个重要的接口IMetadataAware
查看>>
java基础知识——网络编程、IO流
查看>>
“层云”架构有望打破云计算瓶颈
查看>>
Gartner公布2017年顶级安全技术
查看>>
怎么学python?
查看>>
GitLab-CI持续集成(CI)的介绍与运行机制
查看>>
CNCC 2017大会第一天,邱成桐,梅宏,沈向洋,李飞飞,汤道生,马维英都讲了什么?...
查看>>
医疗+AI面临哪些机遇和挑战?阿里健康、飞利浦、辉瑞、阿斯利康的高管们如是说...
查看>>