标签:parent fine 城市 mem javascrip content data- hid mod
题目传送:POJ - 3311 - Hie with the Pie
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int n;
int mp[15][15];
int dp[1300][13];//dp[state][i]表示到达i点状态为state的最短距离
void floyd() {
for(int k = 0; k <= n; k ++) {
for(int i = 0; i <= n; i ++) {
for(int j = 0; j <= n; j ++) {
if(mp[i][j] > mp[i][k] + mp[k][j]) {
mp[i][j] = mp[i][k] + mp[k][j];
}
}
}
}
}
int main() {
while(scanf("%d", &n) != EOF) {
if(n == 0) break;
for(int i = 0; i <= n; i ++) {
for(int j = 0; j <= n; j ++) {
scanf("%d", &mp[i][j]);
}
}
floyd();
memset(dp, 0x3f, sizeof(dp));
for(int state = 0; state < (1 << n); state ++) {//枚举全部状态
for(int i = 1; i <= n; i ++) {
if(state & (1 << (i - 1))) {//状态中已经经过了城市i
if(state == (1 << (i - 1))) {//状态中仅仅经过城市i
dp[state][i] = mp[0][i];
}
else {//状态中除了经过了城市i还经过了其它城市
for(int j = 1; j <= n; j ++) {
if(i != j && state & (1 << (j - 1))) {
dp[state][i] = min(dp[state][i], dp[state ^ (1 << (i - 1))][j] + mp[j][i]);
}
}
}
}
}
}
int ans = INF;
for(int i = 1; i <= n; i ++) {
ans = min(ans, dp[(1 << n) - 1][i] + mp[i][0]);
}
printf("%d\n", ans);
}
return 0;
}
标签:parent fine 城市 mem javascrip content data- hid mod
原文地址:http://www.cnblogs.com/clnchanpin/p/7366538.html