您的位置:首页 > 其它

hdu 5418 Victor and World 状态压缩+SPFA最短路

2016-08-09 01:55 429 查看
传送门:hdu 5418Victor and World

题目大意

给一个地图,从其中一个点开始走,遍历完所有的点后最后再回到这个点,求最短路径

解题思路

这个点数知识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 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: