hdu 5418 Victor and World 最短路 floyd 解题报告
来源:互联网 发布:橡皮砖淘宝店铺 编辑:程序博客网 时间:2024/05/29 04:56
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 the vi -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 is1 , 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.
Victor now is at the country whose number is
Input
The first line of the input contains an integer T , denoting the number of test cases.
In every test case, there are two integersn and m in the first line, denoting the number of the countries and the number of the flights.
Then there arem 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 .
In every test case, there are two integers
Then there are
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
13 21 2 21 3 3
Sample Output
10
Source
BestCoder Round #52 (div.2)
思路:http://blog.csdn.net/qq_21899803/article/details/47903197
代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 17#define inf 0x3f3f3f3fint Map[maxn][maxn],dp[maxn][1<<maxn|1],n;void floyd(){ for(int k=1;k<=n;++k) for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(Map[i][j]>Map[i][k]+Map[k][j]) Map[i][j]=Map[i][k]+Map[k][j];}int main(){ int T,x,y,z,i,j,k,m; cin>>T; while(T--) { scanf("%d%d",&n,&m); memset(Map,inf,sizeof(Map)); for(i=0;i<m;++i){ scanf("%d%d%d",&x,&y,&z); if(z<Map[x][y]) Map[x][y]=Map[y][x]=z; } floyd(); memset(dp,inf,sizeof(dp)); dp[1][1]=0; for(i=1;i<(1<<n);++i) //依次枚举各种状态 for(j=1;j<=n;++j) { if(i&(1<<(j-1))) //枚举该状态下访问的顶点 { for(k=1;k<=n;++k) //枚举访问的其它顶点 if(i&(1<<(k-1))&&j!=k) dp[j][i]=min(dp[j][i],dp[k][i^(1<<(j-1))]+Map[k][j]); } } int ans=inf; for(i=2;i<=n;++i) ans=min(ans,dp[i][(1<<n)-1]+Map[i][1]); printf("%d\n",n==1?0:ans); } return 0;}
0 0
- hdu 5418 Victor and World 最短路 floyd 解题报告
- HDU 5418 Victor and World(状压dp、最短路)
- hdu 5418 Victor and World 状态压缩+SPFA最短路
- hdu 5418 Victor and World 状态压缩dp spfa最短路 floyed最短路
- HDU Victor and World (最短路+状态压缩)
- HDU 5418 Victor and World(状压dp、floy最短路)
- hdu 5418 Victor and World (floyd+状压dp)
- hdu 5418 Victor and World(floyd+状压dp)
- hdu 5418 Victor and World
- hdu 5418 Victor and World
- hdu2544 最短路(floyd) 解题报告
- hdu 5418 Victor and World 状压DP
- hdu 5418 Victor and World【状态压缩】
- hdu 5418 Victor and World(dp)
- HDU-5418 Victor and World(状压+spfa)
- hdu5418--Victor and World(floyd+状压dp)
- HDU 5418 Victor and World(状态压缩路径DP或+floyd 两种方法 求 玈行商问题)经典
- hdu 2544 最短路 spfa 解题报告
- Android-ListView详解(SimpleAdapter,ArrayAdapter,BaseAdapter,OnItenClickListener,OnScrollListener)
- LeetCode_ZigZag Conversion_006
- linux ngingx一些操作
- greenDAO系列2--如何开始
- Druid连接池四(学习笔记)
- hdu 5418 Victor and World 最短路 floyd 解题报告
- 去哪网实习总结:JavaWeb中文传参乱码问题的解决(JavaWeb)
- [MFC学习笔记]--string类,路径层
- Linux内核工程导论——数据结构:树
- 一个简单的反汇编引擎的实现小记
- 冒泡函数
- java1.8环境配置(win8)
- 数据结构之栈的链表实现
- 利用BufferedInputStream bufferedOutputStream读写文件