标签:style blog http color io os ar java for
2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
100 90 7
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 int tri[] = {0,1,3,9,27,81,243,729,2187,6561,19683,59049}; 18 int e[12][12],dp[59049][12],s[59049][12] = {0}; 19 int main() { 20 int n,m,x,y,z; 21 for(int i = 0; i < 59049; i++) { 22 int tmp = i; 23 for(int k = 1; k <= 10; k++) { 24 s[i][k] = tmp%3; 25 tmp /= 3; 26 } 27 } 28 while(~scanf("%d %d",&n,&m)) { 29 for(int i = 1; i <= n; i++) 30 for(int j = 1; j <= n; j++) e[i][j] = INF; 31 for(int i = 0; i < m; i++) { 32 scanf("%d %d %d",&x,&y,&z); 33 if(e[x][y] > z) e[x][y] = e[y][x] = z; 34 } 35 for(int i = 0; i < tri[n+1]; i++) 36 for(int j = 1; j <= 10; j++) dp[i][j] = INF; 37 for(int i = 0; i <= 10; i++) dp[tri[i]][i] = 0; 38 int ans = INF; 39 for(int i = 0; i < tri[n+1]; i++) { 40 bool flag = true; 41 for(int j = 1; j <= n; j++) { 42 if(s[i][j] == 0) flag = false; 43 for(int k = 1; k <= n; k++){ 44 if(j == k || e[j][k] == INF || s[i][k] == 2) continue; 45 int ns = i + tri[k]; 46 dp[ns][k] = min(dp[ns][k],dp[i][j] + e[j][k]); 47 } 48 } 49 for(int k = 1; flag && k <= n; k++) 50 ans = min(ans,dp[i][k]); 51 } 52 ans == INF?puts("-1"):printf("%d\n",ans); 53 } 54 return 0; 55 }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/crackpotisback/p/4004924.html