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,最后一维表示拿到钥匙的状态 ...
随机推荐
- 开启VS的JavaScript调试
前提条件,设置我们的IE 去掉勾,即启用调试然后再JS代码片段中输入debugger.注意:如果IE开启了F12调试面板, VS是不会捕获JS代码片段中的debugger断点.
- Django之QuerySet 创建对象
在前面的模型介绍中设置了3个对象,出版商(publisher),作者(Authro),书籍(book).首先我们在网页中添加各个对象信息填写的界面.填写后点击提交.将会传递给后端.传递方式采用post ...
- .NET 反射
反射是.NET很强大的一个机制. 它就像照妖镜一般的存在.它能调用你的任意私有成员,如:私有构造函数.私有方法.私有字段. 类的构造函数声明为了private,别人无法实例化对象出来?No,No,No ...
- P2770 航空路线问题
\(\color{#0066ff}{题目描述}\) 给定一张航空图,图中顶点代表城市,边代表 2 城市间的直通航线.现要求找出一条满足下述限制条件的且途经城市最多的旅行路线. (1)从最西端城市出发, ...
- 9.path Sum III(路径和 III)
Level: Easy 题目描述: You are given a binary tree in which each node contains an integer value. Find t ...
- FPGA基础学习(1) -- FFT IP核(Quartus)
为了突出重点,仅对I/O数据流为steaming的情况作简要说明,以便快速上手,有关FFT ip核模型及每种设置详细介绍请参考官方手册FFT MegaCore Function User Guide. ...
- bootstrap-table 选择行,并且获得选中行的所有数据内容
html代码如下: <table id="table" data-toggle="table" th:attr="data-url=@{/vie ...
- 【Groovy】Spock with Maven
已经在项目里使用Groovy/Spock做测试框架了,感觉和Maven结合在一起还是挺好用的. 在Maven的pom.xml里引入他们还是挺方便的,第一先要在dependency 里引入 <de ...
- 设定计划的SMART原则
SMART: Specific,Measurable, Attainable,Result-based, Time-based 设定计划,如果不去执行,设定目标,而不去实现,那也算是背叛自己的诺言,终 ...
- 3.数据校验和SpringEL
1.数据验证 数据验证不应该被限定在web层去处理,他应该在任何需要做数据验证的地方做验证: 基于以上考虑,Spring设计了一个既方便又可以在所有层使用的Validator接口 Spring提供了V ...