POJ - 3311 Hie with the Pie

来源:互联网 发布:linux vi编辑怎么换行 编辑:程序博客网 时间:2024/06/07 07:25

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;}


0 0
原创粉丝点击