POJ - 3311 (floyd +-shaped pressure dp)

Topic: the Click
meaning of the questions: to find a way through n points back to the minimum distance of the zero point.
Given adjacency matrix is determined there is no shortest distance between each of the two points, since each point may be repeated a plurality of times calculation, previously treated with the shortest floyd. Then shaped pressure dp.

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAXN=1e7+5;
int n;
int a[15][15];
int dp[15][(1<<12)];
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i,j,k;
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(i=0;i<=n;i++)//中转节点
        {
            for(j=0;j<=n;j++)
            {
                for(k=0;k<=n;k++)
                {
                    a[j][k]=min(a[j][k],a[j][i]+a[i][k]);
                }
            }
        }
        int yyyy=(1<<n+1);
        memset(dp,inf,sizeof(dp));
        dp[0][(1<<i)]=0;
        for(i=1;i<=n;i++)
        {
            int temp=(1<<0)+(1<<i);
            dp[i][(1<<i)]=a[0][i];
        }
        for(i=0;i<yyyy;i++)
        {
            for(j=0;j<=n;j++)
            {
                if((1<<j)&i==0||dp[j][i]==inf)
                    continue;
                for(k=0;k<=n;k++)
                {
                    if(j==k)
                        continue;
                    if((1<<k)&i)
                    {
                        dp[k][i]=min(a[j][k]+dp[j][i],dp[k][i]);
                    }
                    else
                    {
                        int temp=(1<<k)|i;
                        dp[k][temp]=min(dp[k][temp],dp[j][i]+a[j][k]);
                    }
                }
            }
        }
        yyyy--;
        printf("%d\n",dp[0][yyyy]);
    }
    return 0;
}

Published 72 original articles · won praise 19 · views 7489

Guess you like

Origin blog.csdn.net/weixin_43958964/article/details/105245790