ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World
Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
countries on the earth, which are numbered from
to
. They are connected by
undirected flights, detailedly the
-th flight connects the 
-th and the 
-th country, and it will cost Victor's airplane 
L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.
Victor now is at the country whose number is
, he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country.
Input
, denoting the number of test cases. In every test case, there are two integers
and
in the first line, denoting the number of the countries and the number of the flights.
Then there are
lines, each line contains three integers 
, 
and 
, describing a flight.





.





.









.







.








.
Output
lines : the
-th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.Sample Input
1
3 2
1 2 2
1 3 3
Sample Output
10
/*/
一开始题目没读仔细,以为是一个最小树,秒WA一发;
后来想半天发现这个有环,就不是最小树了,搜了一下是Floyd+dp状压。 写完之后一直发现输出的是INF=0x3f3f3f3f ,找了半天最后想到二进制标记状态这里,maps标记用0 0开始会舒服好多。改过来就对了。题目很有意思。。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"string"
#include"cstdio"
#include"vector"
#include"cmath"
#include"queue"
using namespace std;
typedef long long LL;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define MX 401
#define INF 0x3f3f3f3f int maps[20][20];
int dp[200000][20],vis[20]; void init() { memset(maps,0x3f);
memset(vis,0x3f);
memset(dp ,0x3f);
} int main() {
int T;
scanf("%d",&T);
while(T--) {
init();
int n,m,u,v,w;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%d%d%d",&u,&v,&w);
if(maps[--u][--v] > w) //状态压缩从0开始会好写一些
maps[v][u]=maps[u][v]= w;
}
for(int i=0; i<n; i++)maps[i][i]=0; //标记自己到自己距离为0 //后面会要加到这个数字。。
for(int k=0; k<n; k++) {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
maps[i][j]=min(maps[i][j],maps[i][k]+maps[k][j]); //Floyd 把去某一点的最小路程计算出来
}
}
}
dp[1][0]=0;
vis[0]=0;
m=1<<n;
for(int i=1; i<m; i++) {
for(int j=0; j<n; j++) {
if(dp[i][j]==INF)continue;
for(int k=0; k<n; k++) {
if(i&(1<<k)||maps[j][k]==INF)continue; //二进制 1 表示该点走过,0表示没走过,第二维表示现在所在的点。压缩状态
if( dp[i|(1<<k)][k]>dp[i][j]+maps[j][k]) {
dp[i|(1<<k)][k]=dp[i][j]+maps[j][k];
vis[k]=min(vis[k],dp[i|(1<<k)][k]); //比较,去到下一点需要的最小路
}
}
}
}
int minn=1e9+100;;
for(int i=0; i<n; i++) {
minn=min(minn,dp[m-1][i]+vis[i]);
}
printf("%d\n",minn);
}
return 0;
}
ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩的更多相关文章
- POJ3311Hie with the Pie(floyd传递+DP,状态压缩)
问题 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- hdu 4352 XHXJ's LIS (数位dp+状态压缩)
Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully readin ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 5418——Victor and World——————【状态压缩+floyd】
Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Other ...
- HDU 5418 Victor and World 允许多次经过的TSP
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...
- HDU 5418 Victor and World(状压DP+Floyed预处理)
Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Other ...
- HDU 5418 Victor and World (状态压缩dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...
随机推荐
- C++11的模板新特性-变长参数的模板
这个特性很赞,直接给例子吧,假如我要设计一个类,CachedFetcher内部可能使用std::map也可能使用std::unordered_map,也可能是其它的map,怎么设计呢?没有C++11变 ...
- ActiveMQ的几种集群配置
ActiveMQ是一款功能强大的消息服务器,它支持许多种开发语言,例如Java, C, C++, C#等等.企业级消息服务器无论对服务器稳定性还是速度,要求都很高,而ActiveMQ的分布式集群则能很 ...
- My97DatePicker使用技巧
My97DatePicker使用是很常用的控件,总结一下常用使用技巧: 1.onpicked是事件,也就选择日期之后触发事件: 2.isShowClear:是否显示清理按钮: 3.maxDate:最大 ...
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
- poj 1004:Financial Management(水题,求平均数)
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126087 Accepted: ...
- java 杂物间 (一) Mybatis
这里放置的是一些杂物,生人勿入. Token的一般parse 过程. @Test public void shouldDemonstrateGenericTokenReplacement() { Ge ...
- golang基础知识之文件操作
读取文件所有内容以及获得文件操作对象 package mainimport ( "bufio" "fmt" "io" "io/io ...
- 提高WPF程序性能的几条建议
这篇博客将介绍一些提高WPF程序的建议(水平有限,如果建议有误,请指正.) 1. 加快WPF程序的启动速度: (1).减少需要显示的元素数量,去除不需要或者冗余的XAML元素代码. (2).使用UI虚 ...
- [JavaCore]JAVA中的泛型
JAVA中的泛型 [更新总结] 泛型就是定义在类里面的一个类型,这个类型在编写类的时候是不确定的,而在初始化对象时,必须确定该类型:这个类型可以在一个在里定义多个:在一旦使用某种类型,在类方法中,那么 ...
- psql-02基本语法
客户端 数据库: 创建:createdb mydb; 删除: dropdb mydb; 连接: 连接: psql mydb; 断开连接: \q 查看当前版本: select version(); 直接 ...