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 ...
随机推荐
- ASP.NET MVC中的Global.asax文件
1.global.asax文件概述 global.asax这个文件包含全局应用程序事件的事件处理程序.它响应应用程序级别和会话级别事件的代码. 运行时, Global.asax 将被编译成一个动态生成 ...
- 解决Pyqt打包后运行报错:应用程序无法启动 因为程序的并行配置不正确
做了一个生成二维码的小程序:http://www.cnblogs.com/dcb3688/p/4241048.html 直接运行脚本没问题,用pyinstaller打包后再运行就直接报错了: 应用程序 ...
- Linux中exec()执行文件系列函数的使用说明
函数原型: 描述: exec()系列函数使用新的进程映像替换当前进程映像. 工作方式没有什么差别, 只是参数传递的方式不同罢了. 说明: 1. 这6个函数可分为两大类: execl( ...
- scala的tcp通信
client: object ActorClient extends App { import actors.Actor, actors.remote.Node, actors.remote.Remo ...
- Visual Studio Code 1.0发布:100+语言,300+pull请求,1000+扩展
在第一个预览版发布一年后,微软发表了Visual Studio Code 1.0. 在//BUILD 2015大会上,微软宣布,他们的一个团队需要几个月来创建Visual Studio Code的第一 ...
- Python3 基本数据类型注意事项
Python3 基本数据类型 教程转自菜鸟教程:http://www.runoob.com/python3/python3-data-type.html Python中的变量不需要声明.每个变量在使用 ...
- Tips for OpenMesh
OpenMesh 求两点之间的距离 MyMesh::Point p1(1,2,3); MyMesh::Point p2(1,2,5); double d=(p1-p2).length();
- Asp.Net MVC中DropDownListFor的用法
在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...
- autoprefixer
自动化补全工具,在写兼容的css样式的时候,自动补全-webkit,-moz等 sublime和websotrm上都可以安装此工具.
- sql 数字转人民币大写函数(两种方法)
,)) returns @rmb table( 亿 ) ,仟万 ) ,佰万 ) ,拾万 ) ,万 ) ,仟 ) ,佰 ) ,拾 ) ,元 ) ,角 ) ,分 )) as begin insert in ...