您的位置:首页 > 其它

POJ - 3311 Hie with the Pie

2016-07-28 10:23 281 查看

1.题面

http://poj.org/problem?id=3311

2.题意

给你n个点,已知两两之间的距离,让你用最短的时间走完n个点,一个点可以走多次,求最短时间

3.思路

瞎写了一发,居然过了。。。。

4.代码

/*****************************************************************
> File Name: Cpp_Acm.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年07月28日 星期四 08时57分03秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

template<class T>void PrintArray(T* first,T* last,char delim=' '){
for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}

const int debug = 1;
const int size = 10 + 10 ;
const int INF = INT_MAX>>1;
typedef long long ll;

int n;
int dist[size][size];

void floyd(){
for (int k=0;k<n;k++){
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
}

int dp[20][1<<11];

int main()
{
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j;
while (cin >> n){
if (n==0) break;
n++;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
cin >> dist[i][j];
}
}
floyd();
for (i=0;i<20;i++){
for (j=0;j<=(1<<n);j++){
dp[i][j] = INF;
}
}
dp[0][(1<<n)-1] = 0;
for (int s=(1<<n);s>=0;s--){
for (int u=0;u<n;u++){
for (int v=0;v<n;v++){
if (!(s&(1<<v)))
dp[u][s] = min(dp[u][s],dp[v][s|(1<<v)]+dist[v][u]);
}
}
}
cout << dp[0][0] << endl;
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: