hdu 5418 Victor and World 状态压缩+SPFA最短路
2016-08-09 01:55
429 查看
传送门:hdu 5418Victor and World
我们用dp[i][j] 表示 状态为i的时候,结尾在j位置,状态转移方程为dp[S|(1<
题目大意
给一个地图,从其中一个点开始走,遍历完所有的点后最后再回到这个点,求最短路径解题思路
这个点数知识16所以很容易想到状态压缩!我们用dp[i][j] 表示 状态为i的时候,结尾在j位置,状态转移方程为dp[S|(1<
AC
#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; int dp[1<<17][17]; int mp[17][17]; int dis[17]; bool vis[1<<17][17]; int n; int main() { int T , m ; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(mp,0x7f,sizeof(mp)); for(int i =0 ; i < n ;i++) mp[i][i] = 0; for(int i =0 ; i < m ;i++) { int u , v , d; scanf("%d%d%d",&u,&v,&d); u--,v--; mp[u][v] = mp[v][u] = min(mp[u][v],d); } memset(dp,0x7f,sizeof(dp)); dp[0][0] = 0 ; queue<pair<int,int> > q; q.push(make_pair(0,0)); while(!q.empty()) { int s = q.front().first; int u = q.front().second; q.pop(); for(int i = 0 ; i < n ; i++) { int ss = s|(1<<i); if(dp[ss][i]>dp[s][u] + mp[u][i]){ dp[ss][i] = dp[s][u] + mp[u][i]; q.push(make_pair(ss,i)); } } } printf("%d\n",dp[(1<<n)-1][0]); } return 0 ; }
相关文章推荐
- Hdu 4568 Hunter【spfa最短路 tsp状态压缩】
- hdu 5418 Victor and World 状态压缩dp spfa最短路 floyed最短路
- hdu 4640 多校第四场 状态压缩+最短路+dp
- hdu 4114 状态压缩+最短路
- HDU 5418 Victor and World (状态压缩dp)
- BestCoder Round #52 (div.2) HDU 5418 Victor and World (DP+状态压缩)
- HDU 5418 TSP + 状态压缩
- 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter
- HDU Victor and World (最短路+状态压缩)
- hdu 3247(ac自动机+状态压缩dp+最短路)
- HDU 5418——Victor and World——————【状态压缩+floyd】
- HDU Victor and World (最短路+状态压缩)
- HDU 4634 Swipe Bo 状态压缩+BFS最短路
- hdu 5418 Victor and World 状态压缩dp,旅行商问题
- hdu 5418(状态压缩dp+Floyd)
- hdu (4396:多校第十场):又见spfa求各种状态的最短路
- HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
- HDU 5418 Victor and World(状态压缩路径DP或+floyd 两种方法 求 玈行商问题)经典
- hdu 5418 状态压缩dp/tsp问题
- hdu 4114 Disney's FastPass(最短路+状态压缩)