博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 3001 Travelling
阅读量:4648 次
发布时间:2019-06-09

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

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

题意:

n个城市,m条路径,每条城市最多经过2遍,求n个城市至少走一遍,最少花费多少。
方法:
3进制状态压缩,之后DP
代码:

#include 
using namespace std;typedef long long LL;LL f[60000][20];bool v[60000][20];int n,m;LL c[22][22];LL cf[300];inline bool scan_d(int &num) { char in;bool IsN=false; in=getchar(); if(in==EOF) return false; while(in!='-'&&(in<'0'||in>'9')) in=getchar(); if(in=='-'){ IsN=true;num=0;} else num=in-'0'; while(in=getchar(),in>='0'&&in<='9'){ num*=10,num+=in-'0'; } if(IsN) num=-num; return true;}void init(){ cf[0]=1; for(int i=1;i<12;i++) cf[i]=cf[i-1]*3;}LL count(int s[]){ LL ans=0; for(int i=n-1;i>=0;i--){ // cout<
<<" "<
<<" "<
<
0){ s[num++]=x%3; x/=3; }}LL dfs(LL x,int y){// cout<
<<" "<
<
=2) continue; s[i]++; LL np=count(s); // cout<<" \t\t"<
<<" "<<" "<
<<" "<
<<" "<
<
ans) zans=ans; s[i]--; } f[x][y]=zans; return zans;}int main(){ init(); while(scan_d(n)){ scan_d(m); for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) c[i][j]=-1; for(int i=1;i<=m;i++){ int l,r,w; scan_d(l); scan_d(r); scan_d(w); l--;r--; if(c[l][r]==-1||c[l][r]>w) c[l][r]=w; if(c[r][l]==-1||c[r][l]>w) c[r][l]=w; } LL zans=0x3f3f3f3f; int s[10]; for(int k=0;k

  

转载于:https://www.cnblogs.com/xfww/p/7856658.html

你可能感兴趣的文章
INNO SETUP 获得命令行参数
查看>>
链接全局变量再说BSS段的清理
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>
React接入Sentry.js
查看>>
ssh自动分发密匙脚本样板
查看>>
转 小辉_Ray CORS(跨域资源共享)
查看>>
Linux安装postgresql
查看>>
MyBatis启动:MapperStatement创建
查看>>
【 全干货 】5 分钟带你看懂 Docker !
查看>>
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
Java Web项目结构
查看>>
lambda表达式树
查看>>
二次注入原理及防御
查看>>
会话记住已登录功能
查看>>
Linux内核分析——可执行程序的装载
查看>>