HDU 5418——Victor and World——————【状态压缩+floyd】
Victor and World
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 891 Accepted Submission(s): 399
Victor now is at the country whose number is 1, 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.
In every test case, there are two integers n and m in the first line, denoting the number of the countries and the number of the flights.
Then there are m lines, each line contains three integers ui, vi and wi, describing a flight.
1≤T≤20.
1≤n≤16.
1≤m≤100000.
1≤wi≤100.
1≤ui,vi≤n.
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int d[20][20],dp[(1<<20)][20];
int main(){
int t,n,m,a,b,c;
scanf("%d",&t);
while(t--){
memset(d,INF,sizeof(d));
memset(dp,INF,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
if(d[a][b]>c){
d[a][b]=d[b][a]=c;
}
}
for(int i=1;i<=n;i++)
d[i][i]=0;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(d[i][k]<INF&&d[k][j]<INF)
d[i][j] = d[i][j]<(d[i][k]+d[k][j])?d[i][j]:(d[i][k]+d[k][j]);
}
}
}
dp[1][1]=0;
for(int s=1;s<(1<<n);s++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(( (s&(1<<(i-1)))==0 ) &&( (s&(1<<(j-1))) ))
dp[s|(1<<(i-1))][i]=min(dp[s|(1<<(i-1))][i],dp[s][j]+d[j][i]);
}
}
}
int ans=INF;
for(int i=1;i<=n;i++){
ans=min(ans,dp[(1<<n)-1][i]+d[i][1]);
}
printf("%d\n",ans);
}
return 0;
}
HDU 5418——Victor and World——————【状态压缩+floyd】的更多相关文章
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 5418 Victor and World (状态压缩dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...
- 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 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
- POJ 2579 Fiber Network(状态压缩+Floyd)
Fiber Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3328 Accepted: 1532 Des ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
随机推荐
- [hdu 1568] Fibonacci数列前4位
2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2 ...
- Python中的可迭代对象
Python中的可迭代对象有:列表.元组.字典.字符串:常结合for循环使用: 判断一个对象是不是可迭代对象: from collections import Iterable isinstanc ...
- ACM 离散化处理
使用STL算法离散化: 思路:先排序,再删除重复元素,然后就是索引元素离散化后对应的值. 1. unique(): 头文件为algorithm unique的作用是“去掉”容器中相邻元素的重复元 ...
- UICollectionView的基本使用 collectionView
#pragma mark -- 创建CollectionView - (void)createCollectionView{ //关闭自适应 self.automaticallyAdjustsScro ...
- IE下 checkbox、radio等标签的label中的img点击无效(点击不能选中)问题解决
类似以下代码,点击图片不能选中checkbox <input id="dw_1" name="shape[]" value="Round&quo ...
- Unity 关节
引言 关节组件一共分为5大类,它们分别是链条关节.固定关节.弹簧关节.角色关节和可配置关节. 链条关节(Hinge Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物 ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_基元类型(二)
[基元类型推荐] 推荐直接使用 FCL 类型. [理由] 编码时不至于困惑string与String的使用.由于C#的stirng(一个关键字)直接映射到System.String(一个 FCL 类型 ...
- http文件上传/下载
package unit; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputSt ...
- sysbench 安装、使用和测试
摘要: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据库的性能测试.目前支持的数据库有MySQL.Oracle和Post ...
- 匿名类与lambda区别
第一种是继承Thread, 重写了Thread.run() getClass()返回的是匿名类 java.long.Thread$1 第二种是lambda, 重写了Runnable.run() ...