鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3001
題意:還是環遊地圖的問題,只不過這回旅行者對自己有着嚴格的要求,地圖上每個點的經過次數不能超過兩次。
思路:依然是狀壓DP問題,根上一道很像,只不過這次對於每個點來說有三種狀態,分別是未經過,經過一次,經過兩次。所以要用三進制的數來進行狀態壓縮,這個關鍵點想明白了其他的和上一道基本一樣了。對於我來說需要注意的是:能夠到達某一個點經過了兩次的狀態的前一個狀態是這個點已經經過了一次的狀態,而不是從來未經過這個點的狀態。
代碼:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF (1<<30)-1
#define maxn 100005
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int Pow[15];
int cost[15][15];
int dp[59500][15];
void init()
{
for(int i=0; i<10; i++)
{
for(int j=0; j<59500; j++)
dp[j][i]=INF;
}
Pow[0]=1;
dp[Pow[0]][0]=0;
for(int i=1; i<=10; i++)
{
Pow[i]=Pow[i-1]*3;
dp[Pow[i]][i]=0;
}
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
cost[i][j]=INF;
return ;
}
int main()
{
int n,m,k,pos;
LL ans;
while(~scanf("%d%d",&n,&m))
{
init();
ans=INF;
for(int i=0; i<m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(cost[x-1][y-1]>z)
{
cost[x-1][y-1]=z;
cost[y-1][x-1]=z;
}
}
for(int i=1; i<Pow[n]; i++)
{
k=i;
pos=0;
while(k)
{
if(k%3==1||k%3==2)
{
for(int ii=0; ii<n; ii++)
{
if(ii==pos)
continue;
if(dp[i-Pow[pos]][ii]+cost[ii][pos]<dp[i][pos])
dp[i][pos]=dp[i-Pow[pos]][ii]+cost[ii][pos];
}
}
k/=3;
pos++;
}
}
for(int i=1;i<Pow[n];i++)
{
bool flag=0;
k=i;
int t=0;
while(k)
{
t++;
if(k%3==0)
flag=1;
k/=3;
}
if(flag==0&&t==n)
{
for(int ii=0;ii<n;ii++)
if(dp[i][ii]<ans)
ans=dp[i][ii];
}
}
if(ans!=INF)
printf("%I64d\n",ans);
else puts("-1");
}
return 0;
}