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

Problem Description
After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to n. They are connected by m undirected flights, detailedly the i-th flight connects the ui-th and thevi-th country, and it will cost Victor's airplane wi 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 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.

 
Input
The first line of the input contains an integer T, denoting the number of test cases.
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.

 
Output
Your program should print T lines : the i-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
 
Source
 
题目大意:给你t组数据,每组数据给出n和m,分别表示n个城市,m条航线,然后会有m行u,v,w分别表示航线的起点和终点以及飞跃这段航线需要的燃油量,问你从城市1至少经过其余所有城市1次最后飞回城市1的的燃油量最少是多少。
 
解题思路:首先去重边,然后跑floyd算出所有城市之间的最短路径dis,注意d[i][i]=0。由于n个数很小,可以看出是道状态压缩题目。我们用dp[s][i]表示当前的城市访问状态是s,最后到达的城市是i的最少燃油耗费。s表示状态,即用二进制表示城市是否到过。状态转移方程为dp[s|(1<<(i-1))][i]=min(dp[s|(1<<(i-1))][i],dp[s][j]+dis[j][i]) i和j应该满足s&(1<<(i-1))==0&& s&(1<<(j-1))!=0。表示s状态还没到过i城市,s状态已经到过j城市。 最后枚举所有终点为1的情况,得到最小值。
 
 
#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】的更多相关文章

  1. ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

    HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & ...

  2. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  3. HDU 5418 Victor and World (状态压缩dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...

  4. HDU 5418 Victor and World 允许多次经过的TSP

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...

  5. HDU 5418 Victor and World(状压DP+Floyed预处理)

    Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Other ...

  6. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

  7. POJ 2579 Fiber Network(状态压缩+Floyd)

    Fiber Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3328   Accepted: 1532 Des ...

  8. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

  9. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

随机推荐

  1. 【C#】 创建和调用webapi

    二,,通过普通的路由调用,,路径写到http://localhost:29920/api/Players  即   Api/controller  为止

  2. Gazebo学习随记4 Actor: 该配合你的演出我视而不见

    在Gazebo仿真中,除了模型model外,还有一种和model并列的类型——actor. 相比于model受物理引擎的作用,actor不受重力等等的影响,可以按照设定的运动轨迹进行运动. <s ...

  3. Blockade(Bzoj1123)

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...

  4. mysql 表压缩

    mysql中经常出现历史表,这些表不会进行修改数据的操作,只有读操作.那么我们可以对其进行压缩处理,缩减磁盘空间.Innodb表和MyISAM表的压缩指令不一样.下面分别来讨论: 一.InnoDB表 ...

  5. GET/POST/g和钩子函数(hook)

    GET请求和POST请求: 1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放在url中,并且是通过`?`的 ...

  6. P2105 K皇后

    题意:$n*m$棋盘放置k个皇后,问几个格子不被攻击 1≤n,m≤20000,1≤k≤500 开set判重暴力$O(n*k)$然而,setMLE了QAQ 正解确实是$O(n*k)$的 以hang[i] ...

  7. SpriteBuilder 不能 Portrait

    最近用最新的SpriteBuilder V1.3.6和Xcode 6.0.1,发现一个bug.就是在使用Xcode6的时候的SpriteBuilder已经在Project settings 里面设置了 ...

  8. CF671A Recycling Bottles 计算几何

    It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can ...

  9. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  10. google ---gson字符串数组用GSON解析然后用逗号隔开拼接,去掉最后一个逗号

    public static void main(String[] args) { String ss= "[{\"type\":\"0\",\&quo ...