标签:contest time include hdu man desc super split should
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3001
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8286 Accepted Submission(s): 2703
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 10+10; 19 20 int n, m; 21 int g[MAXN][MAXN], c[MAXN], dp[200000][10]; 22 23 int main() 24 { 25 c[0] = 1; 26 for(int i = 1; i<=10; i++) //计算三进制 27 c[i] = c[i-1]*3; 28 29 while(scanf("%d%d", &n, &m)!=EOF) 30 { 31 memset(g, -1, sizeof(g)); 32 for(int i = 1; i<=m; i++) 33 { 34 int u, v, w; 35 scanf("%d%d%d", &u, &v, &w); 36 u--; v--; 37 if(g[u][v]!=-1) w = min(w, g[u][v]); 38 g[u][v] = g[v][u] = w; 39 } 40 41 memset(dp, -1, sizeof(dp)); 42 for(int i = 0; i<n; i++) 43 dp[c[i]][i] = 0; 44 45 int ans = INF; 46 for(int s = 1; s<c[n]; s++) 47 for(int j = 0; j<n; j++) 48 { 49 if(dp[s][j]==-1) continue; //此状态不存在, 跳过 50 51 int cnt = 0; 52 for(int k = 0; k<n; k++) 53 { 54 if(j!=k && g[j][k]!=-1 && (s/c[k])%3<2 ) 55 { 56 int tmp = s + c[k]; 57 if(dp[tmp][k]==-1 || dp[tmp][k]>dp[s][j]+g[j][k]) 58 dp[tmp][k] = dp[s][j] + g[j][k]; 59 } 60 61 if((s/c[k])%3) cnt++; //统计状态s下走了多少个点 62 } 63 64 if(cnt==n) ans = min(ans, dp[s][j]); //如果状态s下走完所有点, 则更新答案 65 } 66 67 printf("%d\n", ans==INF?-1:ans); 68 } 69 }
HDU3001 Travelling —— 状压DP(三进制)
标签:contest time include hdu man desc super split should
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7635000.html